将文件上传到blob存储 - 上传时出错

时间:2018-02-05 22:47:55

标签: c# azure-storage-blobs

我试图将照片上传到Blob存储区,但每次尝试上传时,都会给我一个错误。

它不会让我将图像上传到blob存储。

我想要它做的是上传文件,然后将其重定向到新名称。

public async Task<IActionResult> UploadFileToNewsAsync(IFormFile files, AdminViewModel model)

var name = UrlFriendlySEO.Url(model.NameValue);//make this name SEO friendly here.
var filename = name + png;

CloudStorageAccount storageAccount = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(Accountname,KeyValue), true);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference(ContainerValue);

CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
using (var f = System.IO.File.OpenRead(files.FileName))
{
    await blockBlob.UploadFromStreamAsync(f);
}

每次我尝试使用 postman 时,都会给我这个错误。

using (var f = System.IO.File.OpenRead(files.FileName))

我的观点:

<div class="form-group">
            <div class="col-xs-12">
                @Html.LabelFor(u => u.File)
                <input id="file" name="file" type="file" />
            </div>
        </div>

EIDT完成:

//ERROR FROM HERE
            CloudStorageAccount storageAccount = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(Accountname, KeyValue), true);
            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            CloudBlobContainer container = blobClient.GetContainerReference(ContainerValue);

            CloudBlockBlob blockBlob = container.GetBlockBlobReference(name);
            await blockBlob.UploadFromStreamAsync(model.FileToUpload.OpenReadStream());

1 个答案:

答案 0 :(得分:0)

  

每次我和邮递员一起尝试,都会给我这个错误。

萨鲁曼问道,我不清楚你犯了什么错误。

我在我的asp.net核心测试它并且运行良好。我想你可能无法获得你上传的文件。

将输入名称更改为文件,如下所示:

 <input id="file" name="files" type="file" />

您可以参考以下详细代码:

在控制器中:

public async Task<IActionResult> UploadFileToNewsAsync(IFormFile files, AdminViewModel model)
        {
            var name = UrlFriendlySEO.Url(model.NameValue);//make this name SEO friendly here.
            var filename = name + png;           
            CloudStorageAccount storageAccount = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(Accountname, KeyValue), true);
            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            CloudBlobContainer container = blobClient.GetContainerReference(ContainerValue);

            CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
            using (var f = System.IO.File.OpenRead(files.FileName))
            {
                await blockBlob.UploadFromStreamAsync(f);
            }
            return View();
       }

在视图中:

@using (Html.BeginForm("UploadFileToNewsAsync", "Home", FormMethod.Post,new { enctype = "multipart/form-data" }))
{
    <div class="form-group">
        <div class="col-xs-12">
            @Html.LabelFor(u => u.File)
            <input id="file" name="files" type="file" />
        </div>
        <input type="submit" />
    </div>
}
相关问题