Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"UploadFromFileAsync" will not work as the file has not been saved lo… #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions WebApp-Storage-DotNet/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ namespace WebApp_Storage_DotNet.Controllers
using System.Web;
using System.Threading.Tasks;
using System.IO;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.Azure;
using System.Configuration;

/// <summary>
Expand Down Expand Up @@ -63,7 +61,7 @@ public async Task<ActionResult> Index()
{
// Retrieve storage account information from connection string
// How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"].ToString());
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);

// Create a blob client for interacting with the blob service.
blobClient = storageAccount.CreateCloudBlobClient();
Expand All @@ -80,7 +78,7 @@ public async Task<ActionResult> Index()
List<Uri> allBlobs = new List<Uri>();
foreach (IListBlobItem blob in blobContainer.ListBlobs())
{
if (blob.GetType() == typeof(CloudBlockBlob))
if (blob is CloudBlockBlob)
allBlobs.Add(blob.Uri);
}

Expand Down Expand Up @@ -112,7 +110,7 @@ public async Task<ActionResult> UploadAsync()
for (int i = 0; i < fileCount; i++)
{
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(GetRandomBlobName(files[i].FileName));
await blob.UploadFromFileAsync(files[i].FileName, FileMode.Open);
await blob.UploadFromStreamAsync(files[i].InputStream);
}
}
return RedirectToAction("Index");
Expand Down Expand Up @@ -163,9 +161,10 @@ public async Task<ActionResult> DeleteAll()
{
foreach (var blob in blobContainer.ListBlobs())
{
if (blob.GetType() == typeof(CloudBlockBlob))
var blockBlob = blob as CloudBlockBlob;
if (blockBlob != null)
{
await ((CloudBlockBlob)blob).DeleteIfExistsAsync();
await blockBlob.DeleteIfExistsAsync();
}
}

Expand All @@ -182,10 +181,10 @@ public async Task<ActionResult> DeleteAll()
/// <summary>
/// string GetRandomBlobName(string filename): Generates a unique random file name to be uploaded
/// </summary>
private string GetRandomBlobName(string filename)
private static string GetRandomBlobName(string filename)
{
string ext = Path.GetExtension(filename);
return string.Format("{0:10}_{1}{2}", DateTime.Now.Ticks, Guid.NewGuid(), ext);
return $"{DateTime.Now.Ticks:10}_{Guid.NewGuid()}{ext}";
}
}
}