上传多个文件到Azure Blob存储

7
我对Windows Azure还比较陌生。我已经跟着这个教程:教程,成功实现了它的功能。但是对于我想要的应用程序而言,上传多个文件也需要相对快速的完成。
是否可以修改这个教程以支持多文件上传,例如用户可以使用Shift + 单击来选择多个文件?
或者,如果有人知道详细说明上述内容的好教程,也请告诉我。
谢谢任何帮助,感激不尽。
1个回答

9
我建议您查看来自DotNetCurry的教程,该教程展示了如何使用jQuery创建多文件上传来处理将多个文件上传到ASP.NET页面。它是使用ASP.NET 3.5构建的,但如果您使用.NET 4,也不会有太多问题。

关键在于jQuery插件允许您将一组文件上传到服务器。 ASP.NET代码将通过循环遍历Request.Files集合来处理它:
    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/>");
        }
    }

您需要将此代码放入insertButton_Click事件处理程序中,即将Blob创建和上传到Blob存储放在上述代码的if(hpf.ContentLength>0)块中。

因此,伪代码可能如下所示:

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();
    }
}

再次说明,这只是伪代码,所以我假设它应该是这样工作的。我没有测试语法,但我认为它很接近。

希望这有所帮助。祝你好运!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接