Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :)

UPDATE

As suggested I added the code inside my View:

@model RiPSShared.Models.RiPSModels.AgencyNote

<h2>PostFile</h2>

<form action="@Url.Action("PostFile", "AgencyNotes", new { NoteId=Model.aut_id})" method = "post" enctype="multipart/form-data">
    <label for="file1"> File name:</label>
    <input type="file" name="file" id="file1" />
    <input type="submit" value="Submit" />    
</form>

And the full action controller:

 public ActionResult PostFile(HttpPostedFileBase file, int NoteId)
 {
 CloudBlockBlob blockBlob = container.GetBlockBlobReference(path);
 using (Stream fileStream = file.InputStream)
            {
                blockBlob.UploadFromStream(fileStream);
            }
 return RedirectToAction("Index");
}
like image 983
AlexGH Avatar asked Jan 05 '23 03:01

AlexGH


2 Answers

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
}
like image 126
Shyju Avatar answered Jan 07 '23 18:01

Shyju


Also you set your connection string in configuration file and get from that and store file into blob storage first get its container name

     //StorageConnectionString string specified in configuration file
       CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));

        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Retrieve a reference to a container.
        //specified container name
        CloudBlobContainer container = blobClient.GetContainerReference("myBlobcontainer");

        // Create the container if it doesn't already exist.
        container.CreateIfNotExists();

        container.SetPermissions(
        new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

        CloudBlockBlob blockBlob = container.GetBlockBlobReference("myBlob");

        // Create or overwrite the "myblob" blob with contents from a local file.
      using (var fileStream = file.InputStream)
        {
            blockBlob.UploadFromStream(fileStream);
        }
like image 41
Lalji Dhameliya Avatar answered Jan 07 '23 18:01

Lalji Dhameliya