AlexGH AlexGH - 1 month ago 13
C# Question

Upload file into Blob Azure

I'm trying to upload files into blob Azure from an MVC app, I'm following this tutorial, and I'm stuck in this snippet:

using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
{
blockBlob.UploadFromStream(fileStream);
}


How to make
@"path\myfile"
dynamic?? How to retrieve the real path of my file to put it in there? I accept any other suggestion too to upload to blob :)

Answer

HttpPostedFileBase will have the information you need. Specifically the InputStream property will give you the stream

[HttpPost]
public ActionResult PostFile(HttpPostedFileBase file, int NoteId)  
{   
    // Your existing code for azure blob access
    CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");
    // make sure to a null check on file parameter before accessing the InputStream

    using (var s = file.InputStream)
    {
       blockBlob.UploadFromStream(s);
    }
    // to do :Return something
}
Comments