MVC剃刀文件上传

时间:2016-04-03 17:45:14

标签: c# asp.net-mvc azure razor file-upload

字面意思是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();
    }

完全不知道它有什么问题所以任何帮助都将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您缺少提交按钮以提交所选文件,如

 using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  @Html.ValidationSummary();
  <input type="file" name="file" id="fileToUpload" class="lifile" />
  <span class="field-validation-error" id="spanfile"></span>
  <input type="submit" value="Upload File" id="btnSubmit" />
  @ViewBag.Message
}