Azure存储:上载大小为零字节的文件

时间:2021-03-22 19:40:47

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.

当我将图像文件上传到blob时,图像上传显然成功(没有错误)。当我去云存储工作室时,文件就在那里,但是大小为0(零)字节。

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).

在将图像文件上载到存储之前,来自HttpPostedFileBase的Property InputStream看起来很好(图像的大小对应于预期的内容!并且不会抛出任何异常)。

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.

真正奇怪的是,这在其他情况下非常有效(从Power角色上传Power Points甚至其他图像)。调用SetContent方法的代码似乎完全相同,文件似乎是正确的,因为在正确的位置创建了一个零字节的新文件。

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 个解决方案

#1


45  

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!).

HttpPostedFileBase的InputStream的Position属性与Length属性具有相同的值(可能是因为我之前有另一个文件 - 我觉得这很愚蠢!)。

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

我所要做的就是将Position属性设置回0(零)!

I hope this helps somebody in the future.

我希望这有助于将来的某些人。

#2


19  

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.

感谢Fabio提出并解决您自己的问题。我只是想为你所说的内容添加代码。你的建议对我很有用。

        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:

现在您已准备好使用以下内容上传memoryStream:

blockBlob.UploadFromStream(memoryStream);

#1


45  

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!).

HttpPostedFileBase的InputStream的Position属性与Length属性具有相同的值(可能是因为我之前有另一个文件 - 我觉得这很愚蠢!)。

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

我所要做的就是将Position属性设置回0(零)!

I hope this helps somebody in the future.

我希望这有助于将来的某些人。

#2


19  

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.

感谢Fabio提出并解决您自己的问题。我只是想为你所说的内容添加代码。你的建议对我很有用。

        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:

现在您已准备好使用以下内容上传memoryStream:

blockBlob.UploadFromStream(memoryStream);