2016-04-03 132 views
0

字面上全新的MVC的想法,但我想获得一个文件从视图中上传并把它传递给控制器​​,然后上传到蔚蓝的Blob存储。但是,我无法设法获取用户选择的实际传递给控制器​​的文件。我已经看到MVC 4 Razor File Upload并尝试了答案,但它没有解决。MVC剃刀文件上传

我查看该文件选择

@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

显示您的形式为好,你有'是enctype =多部分/格式data'? –

+0

你的html应该在表格里面 –

+0

问题更新 – Andrew

回答

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 
}