2016-04-03 4 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 = Multipart/form-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 
} 
Смежные вопросы