当我将图像文件上传到blob时,图像上传显然成功(无错误)。当我到云存储工作室时,文件在那里,但是大小为0(零)字节。Azure存储:上传的文件大小为零字节
下面是我使用的代码:
// 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似乎是罚款(的图像的大小对应于什么是预期!并没有例外抛出)。
而真正奇怪的是,在其他情况下(上传Power Points或甚至来自Worker角色的其他图像),此功能完美无缺。调用SetContent方法的代码似乎完全相同,并且文件似乎是正确的,因为在正确的位置创建了具有零字节的新文件。
请问有人有什么建议吗?我调试了这个代码几十次,我看不到问题。欢迎任何建议!
感谢
要稍微澄清一下,当您使用的是流工作,检查,以确保您的数据流的位置属性设置为0您加载后无论什么字节进入它。默认情况下,出于某种原因,Stream的位置将被设置为其内容的结尾。 – Dusda 2011-01-24 22:32:17
是的,这是我现在总是意识到的事情,我永远不会放松一秒钟以记住它。谢谢! – 2011-01-24 22:48:50
它呢,谢谢:) – 2015-01-12 14:50:29