Malcolm
Malcolm

Reputation: 12864

File upload MVC

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

Upvotes: 9

Views: 22540

Answers (5)

Brett Rigby
Brett Rigby

Reputation: 6216

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

Upvotes: 7

Ram Khumana
Ram Khumana

Reputation: 842

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

    }

Upvotes: 0

Shrilata Ellaboina
Shrilata Ellaboina

Reputation: 103

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

Upvotes: 2

Daniel A. White
Daniel A. White

Reputation: 190897

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.

Upvotes: 34

Pedro Santos
Pedro Santos

Reputation: 1004

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

    ...
    }

Upvotes: 6

Related Questions