2014-06-18 179 views
0

我想將圖像上載到模擬器Azure Blob存儲帳戶(devstoreaccount1)。這是的index.html代碼:將文件上傳到azure blob存儲時出現FileLoadException

<!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> 

,這是我的網絡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二TIPO 'System.IO.FileLoadException' 在 Microsoft.WindowsAzure.Storage.dll非gestita NEL codice utente

更多資料:Impossibile caricare IL文件öL'組件 'Microsoft.Data.Services.Client,版本= 5.6.0.0,文化=中性 公鑰= 31bf3856ad364e35' O烏納阿爾相對dipendenze。 La definizione di manifesto dell'assembly specificato non corrisponde al riferimento all'assembly。 (Eccezione DA HRESULT:0x80131040)

在這一點上:

container.CreateIfNotExists(); 

請幫我解決這個問題,或者找到的圖片上傳到使用REST API Blob存儲的另一種方法。

回答

0

而不是使用:

container.CreateIfNotExists(); 

嘗試使用:

if (!container.Exists()) 
    container.Create(); 
相關問題