我正在使用Azure Block Blob Storage保留我的文件。這是我的代碼來上傳文件。Azure Block Blob存儲文件上傳奇怪問題
我在同一個請求中爲相同的文件調用兩次下面的方法; 該方法的第一個調用保存文件的預期,但第二次調用將文件保存爲0的長度,所以我不能顯示圖像,並沒有發生錯誤。
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
UploadFile(file);
UploadFile(file);
return View();
}
public static string UploadFile(HttpPostedFileBase file){
var credentials = new StorageCredentials("accountName", "key");
var storageAccount = new CloudStorageAccount(credentials, true);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("images");
container.CreateIfNotExists();
var containerPermissions = new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
};
container.SetPermissions(containerPermissions);
var blockBlob = container.GetBlockBlobReference(Guid.NewGuid().ToString());
blockBlob.Properties.ContentType = file.ContentType;
var azureFileUrl = string.Format("{0}", blockBlob.Uri.AbsoluteUri);
try
{
blockBlob.UploadFromStream(file.InputStream);
}
catch (StorageException ex)
{
throw;
}
return azureFileUrl ;
}
我只是找到下面的解決方案,這是我的奇怪,但它沒有幫助。
Strange Sudden Error "The number of bytes to be written is greater than the specified ContentLength"
任何想法? 謝謝
順便說一句,你爲什麼要調用這個方法兩次? –