Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was developing a file upload in mvc but it will be used in more than one view so I have to applying it as a partial view how can I make it?
Posted

1 solution

Create a new Controller named FileUploadController and add two action result like below
public class FileUploadController : Controller
 {
     //
     // GET: /FileUpload/

     public ActionResult Index()
     {
         return View();
     }
     public ActionResult UploadFile()
     {
         var httpPostedFileBase = Request.Files["FileName"];
         if (httpPostedFileBase != null && httpPostedFileBase.ContentLength > 0)
         {
             string extension = System.IO.Path.GetExtension(httpPostedFileBase.FileName);
             string path1 = string.Format("{0}/{1}", Server.MapPath("~/SavedFiles"),  extension);
             if (System.IO.File.Exists(path1))
                 System.IO.File.Delete(path1);

             httpPostedFileBase.SaveAs(path1);
         }
         ViewData["Status"] = "Success";
         return View("Index");
     }
 }


Right click the Index action and create an index view like below
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
//like wise you can call the fileupload partial view in multiple views
        @Html.Partial("_FileUpload")
    </div>
</body>
</html>


Right click FileUpload folder inside Views folder and create a Partial View(You can check partial view checkbox from the window) named _FileUpload and paste the code like below
@using(Html.BeginForm("UploadFile","FileUpload",FormMethod.Post,new{enctype="multipart/form-data"}))
{
    <input type="file" name="FileName" id="file" style="width:240px"/>
    <input type="submit" value="Upload"/>
}


You can call this partial view in multiple pages like we have called in index view
Hope this helps
 
Share this answer
 
Comments
Mohammad A. Amer 29-Sep-14 4:09am    
okay, but I have a begin form contain the partial view and it make it doesn't work because the "httpPostedFileBase" always equal null.

@using (Html.BeginForm("SuggestedProjectReviewing", "Project", FormMethod.Post, new { id = "submitForm" }))
{
@Html.Partial("_FileUpload")

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Email Company" name="Approve" class="btn btn-default" />
<input type="submit" value="Register Study Data" name="Refuse" class="btn btn-default" />
</div>
</div>
}
Jameel VM 29-Sep-14 4:30am    
please add ,new{enctype="multipart/form-data"}) and also make sure that name="FileName" is same as Request.Files["FileName"];
Jameel VM 29-Sep-14 4:30am    
you are not put enctype in begin form
Mohammad A. Amer 29-Sep-14 4:41am    
I already make it but doesn't work. I have two begin forms as I cleared above and I think that its the problem.
Jameel VM 29-Sep-14 4:45am    
then remove partial view begin form..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900