?
Solved

File Upload in mvc4

Posted on 2014-03-13
8
Medium Priority
?
463 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 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