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
Solved

File Upload in mvc4

Posted on 2014-03-13
8
450 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

808 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