9

With the following markup in my view:

<form action="Categories/Upload" enctype="multipart/form-data" method="post">
    <input type="file" name="Image">
    <input type="submit" value"Save">
</form>

And in my controller:

public ActionResult Upload(FormCollection form)
{
    var file = form["Image"];
}

The value of file is null. If I try it in a different view using a different controller Controller and it works with the same code.

I have VS2008 on Vista, MVC 1.0.

Why?

Malcolm

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Malcolm
  • 12,524
  • 28
  • 89
  • 125

5 Answers5

34

Use HttpPostedFileBase as a parameter on your action. Also, add the AcceptVerb attribute is set to POST.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload(HttpPostedFileBase image)
{
    if ( image != null ) {
        // do something
    }
    return View();
}

This code is quite in the spirit/design of ASP.NET MVC.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 2
    I just spent a couple hours going in circles because my file input tag had an "ID=" attribute, but not a "NAME=" - make sure you include "name=..." or the file will post to the actionresult, but will be null. Hope this helps someone. – Losbear Sep 20 '12 at 02:52
7

Not to be picky here or anything, but here's how the code ought to look, as Daniel is missing a few minor details in the code he's supplied...

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UploadPlotImage(HttpPostedFileBase image)
{    
    if ( image != null ) 
    {        
        // do something    
    }

    return View();
}
Brett Rigby
  • 6,101
  • 10
  • 46
  • 76
6

Try this code:

    public ActionResult Upload()
    {
        foreach (string file in Request.Files)
        {
            var hpf = this.Request.Files[file];
            if (hpf.ContentLength == 0)
            {
                continue;
            }

            string savedFileName = Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory, "PutYourUploadDirectoryHere");
                savedFileName = Path.Combine(savedFileName, Path.GetFileName(hpf.FileName));

            hpf.SaveAs(savedFileName);
        }

    ...
    }
Pedro Santos
  • 974
  • 2
  • 14
  • 23
  • You don't need Request.Files. See this answer: http://stackoverflow.com/questions/765211/file-upload-mvc/765308#765308 – bzlm Jun 23 '09 at 19:30
  • You need Request.Files if you intend to handle multiple file uploads. – Mark Good Mar 31 '13 at 11:47
  • 1
    You also need Request.Files if you posting other form fields. Good solution Pedro. – Tim Jun 28 '13 at 16:22
2

Even I was facing a problem , The value was null in image at

public ActionResult UploadPlotImadge(HttpPostedFileBase image) 

Earlier I didn't add [AcceptVerbs(HttpVerbs.Post)] which I added. Even after adding it, it didn't work because the second part I was missing, enctype="multipart/form-data", needed to be in the form tag ..

Now it works for me ....

Jonathan Spooner
  • 7,682
  • 2
  • 34
  • 41
Shrilata Ellaboina
  • 103
  • 1
  • 3
  • 12
0

try this class and below action and fix folder path in AppSetting.

config:

   <appSettings>
            <add key="UploadFolerPath" value="..Your folder path" />
   </appSettings>

view:-

<form action="Account/AddImage" id="form_AddImage" method="post"   enctype="multipart/form-data">

            <input type="file" id="Img" name="Img" class="required" />

            <input type="submit" value="Upload" id="btnSubmit" />

</form>

Class:-

public class FileUpload
{
    public string SaveFileName
    {
        get;
        set;
    }


    public bool SaveFile(HttpPostedFileBase file, string FullPath)
    {
        string FileName = Guid.NewGuid().ToString();

        FileName = FileName + System.IO.Path.GetExtension(file.FileName);

        SaveFileName = FileName;

        file.SaveAs(FullPath + "/" + FileName);
        return true;
    }
}

//Post Action

    [HttpPost]
    public ActionResult AddImage(FormCollection Form)
    {

        FileUpload fileupload = new FileUpload();
         var image="";

        HttpPostedFileBase file = Request.Files["Img"];

        if (file.FileName != null && file.FileName != "")
        {

            if (upload.ContentLength > 0)
            {

                  fileupload.SaveFile(Request.Files["Img"],    Server.MapPath(AppSetting.ReadAppSetting("UploadFolerPath")));

                image = fileupload.SaveFileName;

                // write here your Add/Save function

                return Content(image);


            }
        }
        else
        {
                   //return....;
        }

    }
Ram Khumana
  • 844
  • 6
  • 14