Azure storage: Uploaded files with size zero bytes

2019-06-15 01:04发布

When I upload an image file to a blob, the image is uploaded apparently successfully (no errors). When I go to cloud storage studio, the file is there, but with a size of 0 (zero) bytes.

The following is the code that I am using:

// These two methods belong to the ContentService class used to upload
// files in the storage.
public void SetContent(HttpPostedFileBase file, string filename, bool overwrite)
{
    CloudBlobContainer blobContainer = GetContainer();
    var blob = blobContainer.GetBlobReference(filename);

    if (file != null)
    {
        blob.Properties.ContentType = file.ContentType;
        blob.UploadFromStream(file.InputStream);
    }
    else
    {
        blob.Properties.ContentType = "application/octet-stream";
        blob.UploadByteArray(new byte[1]);
    }
}

public string UploadFile(HttpPostedFileBase file, string uploadPath)
{
    if (file.ContentLength == 0)
    {
        return null;
    }

    string filename;
    int indexBar = file.FileName.LastIndexOf('\\');
    if (indexBar > -1)
    {
        filename = DateTime.UtcNow.Ticks + file.FileName.Substring(indexBar + 1);
    }
    else
    {
        filename = DateTime.UtcNow.Ticks + file.FileName;
    }
    ContentService.Instance.SetContent(file, Helper.CombinePath(uploadPath, filename), true);
    return filename;
}

// The above code is called by this code.
HttpPostedFileBase newFile = Request.Files["newFile"] as HttpPostedFileBase;
ContentService service = new ContentService();
blog.Image = service.UploadFile(newFile, string.Format("{0}{1}", Constants.Paths.BlogImages, blog.RowKey));

Before the image file is uploaded to the storage, the Property InputStream from the HttpPostedFileBase appears to be fine (the size of the of image corresponds to what is expected! And no exceptions are thrown).

And the really strange thing is that this works perfectly in other cases (uploading Power Points or even other images from the Worker role). The code that calls the SetContent method seems to be exactly the same and file seems to be correct since a new file with zero bytes is created at the correct location.

Does any one have any suggestion please? I debugged this code dozens of times and I cannot see the problem. Any suggestions are welcome!

Thanks

2条回答
贼婆χ
2楼-- · 2019-06-15 01:45

Thanks Fabio for bringing this up and solving your own question. I just want to add code to whatever you have said. Your suggestion worked perfectly for me.

        var memoryStream = new MemoryStream();

        // "upload" is the object returned by fine uploader
        upload.InputStream.CopyTo(memoryStream);
        memoryStream.ToArray();

// After copying the contents to stream, initialize it's position
// back to zeroth location

        memoryStream.Seek(0, SeekOrigin.Begin);

And now you are ready to upload memoryStream using:

blockBlob.UploadFromStream(memoryStream);
查看更多
老娘就宠你
3楼-- · 2019-06-15 01:55

The Position property of the InputStream of the HttpPostedFileBase had the same value as the Length property (probably because I had another file previous to this one - stupid I think!).

All I had to do was to set the Position property back to 0 (zero)!

I hope this helps somebody in the future.

查看更多
登录 后发表回答