1

Am trying to upload an excel sheet in MVC 3.0 using cshtml. In my view, I have two buttons in my page, for each button a different action result. I have implemented the handling of two buttons in my page. However, when i click on the upload button, no files are there when Request.Files is given. Should i add a parameter in the ActionResult of Upload button click? below is my code for

[HttpPost]
[MultiButton(MatchFormKey = "Upload", MatchFormValue = "Upload")]
public ActionResult UploadFile()
{
    TMReportViewModel UploadModel = new TMReportViewModel();
    foreach (string file in Request.Files) 
    {
        HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
        if (hpf.ContentLength == 0) 
            continue;

        string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(hpf.FileName));
              
        hpf.SaveAs(savedFileName); 
     }
     return View(UploadModel);
 }
Archana David
  • 1,354
  • 2
  • 15
  • 26

1 Answers1

0

I was currently working on this using ASP.NET MVC 4.0.

The solution is in HTML which is there:

You have to write

@using (Html.BeginForm("UploadImage", "Image", FormMethod.Post,
                        new { enctype = "multipart/form-data" }))

so that your controller knows that you are passing the image in Request.File.

For more information, look at Uploading and returning files in ASP.NET MVC.

And for ASP.NET MVC 3.0, look at How to upload image using jQuery in ASP.NET MVC 3.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131