1

I have some issue for my c# code for upload some file...in controller file detect null.

My html code

   @using (Html.BeginForm("Index", "UploadHistory",FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="uploadFile" id="uploadFile" />
 <input type="submit" value="Upload File" id="btnSubmit" />
}

and this code for my controller

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase uploadFile)
    {
        // Verify that the user selected a file
        if (uploadFile != null && uploadFile.ContentLength > 0)
        {
            // extract only the fielname
            var fileName = Path.GetFileName(uploadFile.FileName);
            // store the file inside ~/App_Data/uploads folder
            var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
            uploadFile.SaveAs(path);
        }
        // redirect back to the index action to show the form once again
        return RedirectToAction("Index");
    }

Any ideas why my upload file detect null?? i use C# mvc 4 and razor thank you.

[SOLVED] Just error in javascript method post.

Anonim
  • 61
  • 1
  • 13

4 Answers4

2

Refer to this link

Possible copy of

In short

Use

public ActionResult Index(HttpPostedFileBase uploadFile)

Also change

<input type="file" name="file" id="file" />

to

<input type="file" name="uploadFile" id="uploadFile" />

Community
  • 1
  • 1
Arijit Mukherjee
  • 3,817
  • 2
  • 31
  • 51
2

The following should work for you:

Model

public class UploadFileModel 
{
    public UploadFileModel()
    {
        Files = new List<HttpPostedFileBase>();
    }

    public List<HttpPostedFileBase> Files { get; set; }

}

View

@using (Html.BeginForm("Index", "Home",FormMethod.Post, new { enctype = "multipart/form-data" }))
{
 Html.TextBoxFor(m => m.Files.Files, new { type = "file", name = "Files" })
 <input type="submit" value="Upload File" id="btnSubmit" />
}

Controller

[HttpPost]
public ActionResult Index(UploadFileModel model)
{
    var file = model.Files[0];
    return View(model);
}
hutchonoid
  • 32,982
  • 15
  • 99
  • 104
0

I am not sure model binding works in this scenario. Either you need to use HttpPostedFileBase as a parameter to the controller action or you need to use Request.Files option.

Using Request.Files option.

foreach (string file in Request.Files)
{
   HttpPostedFile hpf = Request.Files[file] as HttpPostedFile;
   if (hpf.ContentLength == 0)
      continue;
   string savedFileName = Path.Combine(
      AppDomain.CurrentDomain.BaseDirectory, 
      Path.GetFileName(hpf.FileName));
   hpf.SaveAs(savedFileName);
}

EDIT : Here I found a blog which uses similar scenario (Model binding). It may help you - http://cpratt.co/file-uploads-in-asp-net-mvc-with-view-models/

Anuraj
  • 18,859
  • 7
  • 53
  • 79
0

Use below

[HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {

        if (file != null && file.ContentLength > 0) 
        {

            var fileName = Path.GetFileName(file.FileName);

            var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
            file.SaveAs(path);
        }

        return RedirectToAction("Index");        
    }

mainly use HttpPostedFileBase in your parameter

MSTdev
  • 4,507
  • 2
  • 23
  • 40