?
Solved

File Upload in mvc4

Posted on 2014-03-13
8
Medium Priority
?
471 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
ID: 39925916
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
ID: 39925971
Hi

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

Expert Comment

by:Carl Tawn
ID: 39925999
Have you decorated the controller method with a [HttpPost] attribute?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:praveen1981
ID: 39926081
yes, i decorated it
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39926159
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
ID: 39933685
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
ID: 39933686
No problem. Glad you got it sorted.
0
 

Author Closing Comment

by:praveen1981
ID: 39949796
solved
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

807 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