Azure Blob存储-从Upload Control .NET上传

时间:2018-10-13 11:21:32

标签: c# .net azure azure-storage-blobs

我将文件从Web应用程序上传到Azure Blob存储时遇到问题。一切在开发模式下均能正常运行,但是在发布时无法上传文件。我如何访问要上传的文件?下面是我拥有的代码,这些代码仅在生产环境中有效,而不能在生产环境中使用:

aspx页面:

 <asp:Panel runat="server" ID="PanelUpload">
     <div class="form-group">
         <input type="file" id="myfile" multiple="multiple" name="myfile" runat="server" />
     </div> <br />
     <asp:Button ID="Button_Upload" runat="server" Text="Upload New Images" OnClick="Click_UploadImages" CssClass="btn btn-primary mr-1 mb-1 w-100" />
                <br />
      <asp:Label ID="Span1" runat="server"></asp:Label>
  </asp:Panel>

后面的代码:

    protected void Click_UploadImages(object sender, EventArgs e)
    {
        HttpFileCollection uploadedFiles = Request.Files;
        CloudStorageAccount sa = CloudStorageAccount.Parse(connString);
        CloudBlobClient bc = sa.CreateCloudBlobClient();
        CloudBlobContainer container = bc.GetContainerReference(destContainer);
        container.CreateIfNotExists();

        for (int i = 0; i < uploadedFiles.Count; i++)
        {
            HttpPostedFile userPostedFile = uploadedFiles[i];
            try
            {
                if (userPostedFile.ContentLength > 0)
                {
                    CloudBlockBlob b = container.GetBlockBlobReference(product_id + "-" + i + Path.GetExtension(userPostedFile.FileName));
                    //b.UploadFromFile(userPostedFile.FileName);
                    using (var fs = File.Open(userPostedFile.FileName, FileMode.Open, FileAccess.Read, FileShare.None))
                    {
                        b.UploadFromStream(fs);
                    }

                    //Image Record to SQL After BLOB Upload
                    TraceBlobImageUsingSQL T = new TraceBlobImageUsingSQL();
                    T.ImageSQLEntry(product_id, i, "productimages", b.Name, userPostedFile.ContentLength, Path.GetExtension(userPostedFile.FileName), Context.User.Identity.GetUserId());
                }
            }
            catch (Exception Ex)
            {
                Span1.Text += "Error: <br>" + Ex.Message;
            }
        }
        RenderHTMLPanelData(Request.QueryString["ProductID"]);
    }

1 个答案:

答案 0 :(得分:0)

HttpPostedFile公开了InputStream属性,该属性使您可以直接以流形式读取内容。用它代替打开文件流:

                using (var stream = userPostedFile.InpuStream)
                {
                    b.UploadFromStream(stream);
                }