【发布时间】:2015-07-13 05:56:39
【问题描述】:
我正在使用 Azure 块 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"
有什么想法吗? 谢谢
【问题讨论】:
-
顺便说一句,你为什么要调用这个方法两次?
标签: azure azure-storage azure-blob-storage