当我将图像文件上载到blob时,图像显然已成功上载(没有错误)。当我进入CloudStorageStudio时,文件就在那里,但大小为0(零)字节。
以下是我使用的代码:
// 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));
在将图像文件上载到存储之前,来自httpPostedFileBase的属性inputstream似乎很好(图像的大小与预期的大小相对应!)不会抛出异常)。
而真正奇怪的是,这在其他情况下(从工作者角色上传功率点,甚至其他图像)也能很好地工作。调用setcontent方法的代码似乎完全相同,并且文件似乎是正确的,因为在正确的位置创建了一个零字节的新文件。
有人有什么建议吗?我调试了这段代码很多次,但看不到问题所在。欢迎提出任何建议!
谢谢