0

Literally brand new to the MVC idea, but I'm trying to get a file to upload from the view and pass it to the controller which then uploads to azure blob storage. However I cannot manage to get the file that the user selects to actually pass to the controller. I've seen MVC 4 Razor File Upload and tried the answer but it's not working out.

my View where the file is selected

  @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>

Controller:

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

Got absolutely no idea what's wrong with it so any help would be appreciated thank you!

Community
  • 1
  • 1
Andrew
  • 21
  • 1
  • 3

1 Answers1

0

I think you are missing submit button to submit the selected file like

 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
}
Rica
  • 88
  • 1
  • 12