Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading Multiple Files to Azure Blob Storage

Pretty new to Windows Azure. I've followed this tutorial: tutorial. It works perfectly however one limitation is that for the application I have in mind, it would need to be possible to upload multiple files relatively quickly.

Is it possible to modify the tutorial to support multi-file uploads, e.g. The user can use shift-click to select multiple files..

Or if anyone knows of any good tutorials detailing the above?

Any help is appreciated,

Thanks

like image 584
109221793 Avatar asked Jul 25 '11 19:07

109221793


People also ask

How do I automatically upload files to Azure Blob Storage?

Create Power Automate Desktop Flow Go to containers and create a new container. Open the container and on the and navigate to Shared access signature. Select add, create, and write permission, change the time if needed, and press Generate SAS token and URL. Copy the Blob SAS URL and save it as the variable in the flow.

Which command upload files to a storage blob?

You can upload files and directories to Blob storage by using the AzCopy v10 command-line utility.


1 Answers

I'd take a look at this tutorial from DotNetCurry which shows how to create a multiple file upload using jQuery to handle the multiple uploading of files to an ASP.NET page. It's built using ASP.NET 3.5, but it shouldn't matter if you're using .NET 4 - there's nothing too crazy going on.

But the key is that the jQuery plug-in will allow you to upload a collection of files to the server. The ASP.NET code behind will handle that by looping through the Request.Files collection:

    HttpFileCollection hfc = Request.Files;
    for (int i = 0; i < hfc.Count; i++)
    {
        HttpPostedFile hpf = hfc[i];
        if (hpf.ContentLength > 0)
        {
            hpf.SaveAs(Server.MapPath("MyFiles") + "\\" +
              System.IO.Path.GetFileName(hpf.FileName));
            Response.Write("<b>File: </b>" + hpf.FileName + " <b>Size:</b> " +
                hpf.ContentLength + " <b>Type:</b> " + hpf.ContentType + " Uploaded Successfully <br/>");
        }
    }

You would put this code in your tutorial in the insertButton_Click event handler - essentially putting the blob creation and uploading to blob storage inside the above code's if(hpf.ContentLength>0) block.

So pseudo-code may look like:

protected void insertButton_Click(object sender, EventArgs e)
{
    HttpFileCollection hfc = Request.Files;
    for (int i = 0; i < hfc.Count; i++)
    {
      HttpPostedFile hpf = hfc[i];

      // Make a unique blob name
      string extension = System.IO.Path.GetExtension(hpf.FileName);

      // Create the Blob and upload the file
      var blob = _BlobContainer.GetBlobReference(Guid.NewGuid().ToString() + extension);
      blob.UploadFromStream(hpf.InputStream);

      // Set the metadata into the blob
      blob.Metadata["FileName"] = fileNameBox.Text;
      blob.Metadata["Submitter"] = submitterBox.Text;
      blob.SetMetadata();

      // Set the properties
      blob.Properties.ContentType = hpf.ContentType;
      blob.SetProperties();
    }
}

Again, it's just pseudo-code, so I'm assuming that's how it would work. I didn't test the syntax, but I think it's close.

I hope this helps. Good luck!

like image 156
David Hoerster Avatar answered Oct 01 '22 16:10

David Hoerster