【发布时间】:2016-07-23 04:20:19
【问题描述】:
从字面上看,MVC 理念是全新的,但我正在尝试从视图中获取要上传的文件并将其传递给控制器,然后控制器再上传到 Azure Blob 存储。但是我无法获取用户选择实际传递给控制器的文件。我见过MVC 4 Razor File Upload 并尝试了答案,但没有成功。
选择文件的我的视图
@using (Html.BeginForm("FileView", "Shares", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>FileUploadModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.numshares, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.numshares, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.numshares, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.minshares, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.minshares, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.minshares, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.file, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="file" id="file" />
</div>
</div>
控制器:
[HttpPost]
public ActionResult FileView(FileUploadModel model, HttpPostedFileBase file)
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
ConfigurationManager.AppSettings["StorageConnectionString"]);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("shares");
// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob;
// Create or overwrite the "myblob" blob with contents from a local file.
BinaryReader b = new BinaryReader(file.InputStream);
byte[] binData = b.ReadBytes(model.file.ContentLength);
int numshares = model.numshares;
int minshares = model.minshares;
Share[] share = shares(binData, numshares, minshares);
foreach (Share s in share)
{
String n = model.file.FileName + s.getId().ToString();
blockBlob = container.GetBlockBlobReference(n);
blockBlob.UploadFromByteArray(s.serialize(), 0, s.serialize().Length);
}
return View();
}
public ActionResult FileView()
{
return View();
}
完全不知道它有什么问题,所以任何帮助将不胜感激,谢谢!
【问题讨论】:
-
也显示你的表格,你有
enctype = multipart/form-data吗? -
您的 html 应该在表单中
标签: c# asp.net-mvc azure razor file-upload