Solved

File Upload in mvc4

Posted on 2014-03-13
8
459 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
Industry Leaders: 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!

 

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

Technology Partners: 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 an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
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: …

691 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