简体   繁体   中英

FileLoadException when uploading a file to azure blob storage

I want to upload an image to the emulator Azure Blob Storage account (devstoreaccount1). This is the index.html code:

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

and this is the Post method of my Web Api Controller

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;
        }

So when I run the server and try to upload an image stored in my file system, the application throws this Exception:

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. La definizione di manifesto dell'assembly specificato non corrisponde al riferimento all'assembly. (Eccezione da HRESULT: 0x80131040)

at this point:

container.CreateIfNotExists();

Please help me to fix it or to find another method to upload an image into blob storage using rest api.

Instead of using:

container.CreateIfNotExists();

Try using:

if (!container.Exists())
   container.Create();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM