Uploading Multiple Files to Azure Blob Storage

2019-04-08 12:40发布

问题:

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

回答1:

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!