File upload MVC
Asked Answered
A

5

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

Amadus answered 19/4, 2009 at 10:43 Comment(4)
"no one will have an answer" - ???Edmanda
Well the 2 answers given do not and i put money on that no one solves it on SO.Amadus
Answer is one that solves a problem rightAmadus
Please don't assume the community will fail you.Evonneevonymus
E
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.

Evonneevonymus answered 19/4, 2009 at 12:3 Comment(1)
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.Scend
R
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();
}
Rumple answered 20/6, 2009 at 20:17 Comment(2)
My guess is Daniel was missing something but saw Brett's post and modified his answer.Treacherous
Grammar police! "UploadPlotImadge" >>> "UploadPlotImage" lol =)Scend
B
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);
        }

    ...
    }
Breeches answered 19/4, 2009 at 11:22 Comment(3)
You don't need Request.Files. See this answer: https://mcmap.net/q/1116241/-file-upload-mvc/…Barbrabarbuda
You need Request.Files if you intend to handle multiple file uploads.Exclamatory
You also need Request.Files if you posting other form fields. Good solution Pedro.Lheureux
A
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 ....

Annoying answered 6/12, 2011 at 5:39 Comment(1)
enctype="multipart/form-data" was critical for me as well, why doesn't everyone need this?Mezzanine
G
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....;
        }

    }
Goldman answered 6/12, 2011 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.