加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将多个文件上载到Azure Blob存储

发布时间:2020-12-15 08:18:36 所属栏目:百科 来源:网络整理
导读:对 Windows Azure来说很新鲜.我已经按照本教程: tutorial.它完美地工作但是一个限制是对于我想到的应用程序,需要能够相对快速地上传多个文件. 是否可以修改教程以支持多文件上传,例如用户可以使用shift-click选择多个文件. 或者,如果有人知道任何好的教程详
对 Windows Azure来说很新鲜.我已经按照本教程: tutorial.它完美地工作但是一个限制是对于我想到的应用程序,需要能够相对快速地上传多个文件.

是否可以修改教程以支持多文件上传,例如用户可以使用shift-click选择多个文件.

或者,如果有人知道任何好的教程详细说明上述内容?

任何帮助表示赞赏,

谢谢

解决方法

我将从DotNetCurry看一下这个 tutorial,它展示了如何使用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创建并上传到上面代码的if(hpf.ContentLength> 0)块中的blob存储.

所以伪代码可能看起来像:

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

同样,它只是伪代码,所以我假设它是如何工作的.我没有测试语法,但我认为它很接近.

我希望这有帮助.祝好运!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读