【问题标题】:Azure Blob uploading file to storageAzure Blob 将文件上传到存储
【发布时间】:2017-08-19 13:05:16
【问题描述】:

在我的项目中,我需要用户上传一个文件,然后将其发送到 Blob 存储。目前,我只能发送已经存储的文件,我需要它来发送用户上传的文件。

这是我目前上传文件的代码,但是我不确定如何修改

使用 (var fileStream = System.IO.File.OpenRead(file))

这样它就从“HttpPostedFile 文件”中获取一个文件

代码中被注释掉的部分是将其存储到我的系统而不是云端的旧版本。

    // POST: DocumentUps/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CreateUpload([Bind(Include = "DocumentId,DocName,creationDate,RevisionNumber,Author,Status,ActivationDate,Attachment,RevisionId")] DocumentUps documentUps, HttpPostedFileBase file)
    {


        if (ModelState.IsValid)
        {
            documentUps.RevisionId = documentUps.DocumentId;
            documentUps.Username = User.Identity.Name;
            documentUps.Attachment = file.FileName;
            documentUps.creationDate = DateTime.Now;
            documentUps.ActivationDate = DateTime.Now;
            documentUps.RevisionNumber = 0;
            documentUps.Status = StatusChoice.Draft;

            db.DocumentUps.Add(documentUps);
            db.SaveChanges();

            int id = documentUps.DocumentId;


            DocumentUps docsup = db.DocumentUps.Find(id);

            documentUps.RevisionId = id;
            documentUps.Username = User.Identity.Name;
            documentUps.Attachment = documentUps.Attachment;
            documentUps.DocumentId = documentUps.DocumentId;
            documentUps.creationDate = documentUps.creationDate;
            documentUps.ActivationDate = DateTime.Now;
            documentUps.RevisionNumber = 0;
            documentUps.DocName = documentUps.DocName;
            documentUps.Author = documentUps.Author;
            documentUps.Status = StatusChoice.Draft;

            db.Entry(documentUps).State = EntityState.Modified;
            db.SaveChanges();


            if (file != null && file.ContentLength > 0)
            {

                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("filestorageideagen_AzureStorageConnectionString"));

                Microsoft.WindowsAzure.StorageClient.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                Microsoft.WindowsAzure.StorageClient.CloudBlobContainer container = blobClient.GetContainerReference("documentuploader");

                Microsoft.WindowsAzure.StorageClient.CloudBlockBlob blob = container.GetBlockBlobReference("TestUpload");

                using (var fileStream = System.IO.File.OpenRead(file))
                {
                    blob.UploadFromStream(fileStream);
                }


                //var fileName = documentUps.DocumentId.ToString() + documentUps.RevisionId.ToString() + Path.GetFileName(file.FileName);

                //var path = Path.Combine(Server.MapPath("~/Content/fileHistory"), fileName);
                //file.SaveAs(path);
            }

【问题讨论】:

标签: asp.net database azure azure-blob-storage


【解决方案1】:

正如@Brendan Green 提到的,使用 HttpPostedFile 的 InputStream 属性将解决您的问题。请使用以下代码替换您原来的上传代码。

//set content type of your blob
blob.Properties.ContentType = file.ContentType;
//upload your file to your blob
blob.UploadFromStream(file.InputStream);

Microsoft.WindowsAzure.StorageClient.CloudBlobClient

根据您的代码,您使用的是旧版本的存储客户端库。最新版本是 8.1。如果你是使用 Azure 存储客户端库的初学者。我们建议您使用 NuGet 升级到最新版本。

【讨论】:

  • 多么奇怪,我使用的是最新版本,但在某个控制器上它试图使用旧版本。现在通过清理和重建修复它。这很好用,非常感谢
猜你喜欢
  • 2017-01-24
  • 2019-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
  • 2021-03-07
  • 2011-10-12
  • 2021-04-30
相关资源
最近更新 更多