當我將圖像文件上傳到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