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