LoginSignup
4
1

More than 5 years have passed since last update.

【Azure】ASP.net MVC5で画像ファイルをAzureBLOBStorageに保存する方法【BLOB】

Last updated at Posted at 2018-05-14

Edgeとchrome

input type="file"を使用してfullpathを取得しようとすると、Edgeは表示されるが、chromeでは表示されない為、fullPathを使用するやり方はできません。

MVC5に関して説明している資料は大体が、表示されているfullpathを使用するものがほとんどだったので、ほかのやり方を紹介します。

事前準備

AzureStorageのConnectStringとContainerNamewが必要です。

コード

View

index.html
@using (Html.BeginForm("Add", "Photo", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" id="file" name="uploadFile" multiple/>
    <input type="submit" value="アップロード" />
}

Controller

PhotoController.cs

public class EventPhotoInfoController : Controller
{
    private static readonly string ConnectString = ConfigurationManager.AppSettings.Get("Storage_ConnectString");
    private static readonly string PhotoContainerName = ConfigurationManager.AppSettings.Get("Storage_Container_Photo");

        [HttpPost]
        public ActionResult Add(HttpPostedFileWrapper uploadFile)
        {
            CloudStorageAccount account = CloudStorageAccount.Parse(ConnectString);
            CloudBlobClient blobClient = account.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(PhotoContainerName);
            container.CreateIfNotExistsAsync().Wait();

            var blobName = Guid.NewGuid().ToString("D") + System.IO.Path.GetExtension(uploadFile.FileName);
            CloudBlockBlob blob = container.GetBlockBlobReference(blobName);
            blob.Properties.ContentType = uploadFile.ContentType;
            blob.UploadFromStreamAsync(uploadFile.InputStream);
            return View();
        }
}

参考

Upload Image In Azure Blob Storage With ASP.NET MVC

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1