Solved

File Upload in mvc4

Posted on 2014-03-13
8
444 Views
Last Modified: 2014-03-24
Hi,

  IN MY VIEW,

@using (Html.BeginForm("InsuranceInformation", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {  
            <div>
            <input type="file" name="file" id="file" />
             <input type="submit" value="submit" />
           </div>
    }

And in my Home controller ,
public void InsuranceInformation(HttpPostedFileBase file)
        {
            if (Request != null)
            {                
                foreach (string files in Request.Files)
                {
                    HttpPostedFileBase hpf = Request.Files[files];
                    //Save file here
                }
            }
        }

NOW I am uploading a file but after submitting , in controller I am getting file as null
 Could you please say me what is wrong in it. And I am using asp.net mvc
0
Comment
Question by:praveen1981
  • 4
  • 4
8 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
You need to use the HttpPostedFileBase that you pass to the method rather than request.files collection.
public void InsuranceInformation(HttpPostedFileBase file)
{
    if (file.ContentLength > 0) 
    {
        var fileName = Path.GetFileName(file.FileName);
        var path = Path.Combine(Server.MapPath("~/Path to save to"), fileName);
        file.SaveAs(path);
    }
}

Open in new window

0
 

Author Comment

by:praveen1981
Comment Utility
Hi

actually the  HttpPostedFileBase file  , file is getting null , that is the problem
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
Have you decorated the controller method with a [HttpPost] attribute?
0
 

Author Comment

by:praveen1981
Comment Utility
yes, i decorated it
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
Well, on the face of it everything looks ok. Try this simplified version and see if you get an object reference:
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @using (Html.BeginForm("InsuranceInformation", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <input type="file" id="file" name="file" />
            <input type="submit" />
        }
    </div>
</body>
</html>

Open in new window

And in the Home controller:
[HttpPost]
public ActionResult InsuranceUpload(HttpPostedFileBase file)
{
     return View();
}

Open in new window

Put a breakpoint at the start of the method and see if file contains anything when posted back.
0
 

Accepted Solution

by:
praveen1981 earned 0 total points
Comment Utility
Hi

I solved this problem, it is basically inheriting the layout page , this layout is jquery mobile page so either i have to add layout =null or i have to add

@data_ajax = "false"  to my Html.BeginForm method.

any how thanks for your response
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
No problem. Glad you got it sorted.
0
 

Author Closing Comment

by:praveen1981
Comment Utility
solved
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now