简体   繁体   中英

Azure Blob uploading file to storage

In my project, I need the user to upload a file, this is then sent to Blob storage. Currently, I can only send files already stored, I need it to send files that the user uploads.

Here is my current code to upload the file, however, I am unsure how to modify

using (var fileStream = System.IO.File.OpenRead(file))

So that It takes a file from the "HttpPostedFile file"

The commented out parts of the code are the old version that stored it onto my system and not the cloud.

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

As @Brendan Green mentioned, using InputStream property of HttpPostedFile will solve your issue. Please use following code to replace your original upload code.

//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

Based on your code, you are using the old version of storage client library. The newest version is 8.1. If you are the starter to use Azure Storage client library. We suggest you upgrade to the latest version by using NuGet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM