我想将图像上传到模拟器Azure Blob存储帐户(devstoreaccount1)。这是 index.html 代码:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>App</title>
</head>
<body>
<div id="body">
<form name="form1" method="post" action="api/photo" enctype="multipart/form-data">
<div>
<label>
Browse File
</label>
<input name="myFile" type="file" />
</div>
<div>
<input type="submit" value="Upload" />
</div>
</form>
</div>
</body>
</html>
这是我的Web Api控制器的Post方法
public HttpResponseMessage Post() {
// Retrieve storage account from connection-string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("CloudStorageConnectionString"));
// Create the blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container
// Container name must use lower case
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
// Create the container if it doesn't already exist
container.CreateIfNotExists();
// Enable public access to blob
var permissions = container.GetPermissions();
if (permissions.PublicAccess == BlobContainerPublicAccessType.Off) {
permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
container.SetPermissions(permissions);
}
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent()) {
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
HttpResponseMessage result = null;
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > 0) {
var docfiles = new List<string>();
foreach (string file in httpRequest.Files) {
var postedFile = httpRequest.Files[file];
var filename = postedFile.FileName;
var blob = container.GetBlockBlobReference(filename);
var filePath = HttpContext.Current.Server.MapPath("~/" + filename);
postedFile.SaveAs(filePath);
docfiles.Add(filePath);
using (var filestream = File.OpenRead(filePath)) {
blob.UploadFromStream(filestream);
}
File.Delete(filename);
}
result = Request.CreateResponse(HttpStatusCode.Created, docfiles);
}
else {
result = Request.CreateResponse(HttpStatusCode.BadRequest);
}
return result;
}
因此,当我运行服务器并尝试上传存储在我的文件系统中的图像时,应用程序会抛出此异常:
Eccezione di tipo'System.IO.FileLoadException'in Microsoft.WindowsAzure.Storage.dll non gestita nel codice utente
Ulteriori informazioni:Impossibile caricare il file o l'assembly 'Microsoft.Data.Services.Client,Version = 5.6.0.0,Culture = neutral, PublicKeyToken = 31bf3856ad364e35'o una delle relative dipendenze。啦 definizione di manifesto dell'assembly specificato non corrisponde al riferimento all'assembly。 (Eccezione da HRESULT:0x80131040)
此时:
container.CreateIfNotExists();
请帮我修复它或找到另一种方法,使用rest api将图像上传到blob存储区。
答案 0 :(得分:0)
而不是使用:
container.CreateIfNotExists();
尝试使用:
if (!container.Exists())
container.Create();