Anonim
Anonim

Reputation: 61

Razor upload file returns null

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.

Upvotes: 1

Views: 3448

Answers (4)

hutchonoid
hutchonoid

Reputation: 33306

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

Upvotes: 2

Anuraj
Anuraj

Reputation: 19598

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/

Upvotes: 0

MSTdev
MSTdev

Reputation: 4635

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

Upvotes: 0

Arijit Mukherjee
Arijit Mukherjee

Reputation: 3875

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" />

Upvotes: 2

Related Questions