簡體   English   中英

Azure Blob將文件上傳到存儲

[英]Azure Blob uploading file to storage

在我的項目中,我需要用戶上傳文件,然后將其發送到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);
            }

正如@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升級到最新版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM