Question

How is the best way to create an file upload function (model, view, controller) using c# MVC 4.5 with razor and Database? Can you put the view and controller code here, please?

Was it helpful?

Solution

I made a tutorial on it, you can see it here on my blog:

http://developmentpassion.blogspot.com/2013/08/aspnet-mvc-ajax-file-uploading-using.html

Here is the code:

 @using (Html.BeginForm("Upload","TestDetails", FormMethod.Post,
    new
    {
        enctype = "multipart/form-data",
        id = "ImgForm",
        name = "ImgForm",
        target = "UploadTarget"
    }))
     {
         <input type="hidden" id="bookingdid" name="bookingdid" value="@Model.BookingDId" />
       <a class="close-reveal-modal"><img alt="close" src="@Url.Content("~/images/crossicon.png")" width="32" /></a>


     <div style="width:100px; margin:auto;float:left;line-height:30px;font-weight:bold;">File</div>


         <div style="width:200px;float:left;margin:auto;">

         <input type="file" id="uploadFile" name="file" data-val="true" data-val-required="please select a file" />
          <span id="validityMessages" style="color:Red;"></span>
         </div>


         <div style="clear:both;"></div>

     <div style="width:auto; margin-top:3%;">
         <div style="margin:5% 18%;">
         <input  id="upload" type="submit" value="Upload" name="Upload" onclick="UploadImage()" />
         <span id="uploadLoader" style="display:none;"><img id="searchLoader" src="../Images/loader.gif" />Uploading Please Wait</span>
         </div>
         </div>



     }
     <div id="uploadsContainer"></div>
     <iframe id="UploadTarget" name="UploadTarget" onload="UploadImage_Complete();" style="position: absolute; left: -999em; top: -999em;"></iframe>

              </div>

My uploads class:

public class Uploads
    {
        public string FilePath { get; set; }
        public long bookingdid { get; set; }
        public string FileName { get; set; }
    }

here is the action:

[HttpPost]
        public ActionResult Upload(FormCollection form,HttpPostedFileBase file)
        {
            //HttpPostedFileBase file = Request.Files[0 ];

            Uploads uploadsModel = new Uploads();

            uploadsModel.bookingdid = long.Parse(form["bookingdid"]);
            uploadsModel.FileName = file.FileName;
            uploadsModel.FilePath = "~/Uploads/" + file.FileName;

            if (file.ContentLength < 1048576)
            {

                if (file != null)
                {
                    file.SaveAs(Server.MapPath(uploadsModel.FilePath));
                }

                TestDetailsViewModel objTestDetails = new TestDetailsViewModel();
                objTestDetails.SaveFile(uploadsModel);
            }

            return PartialView("~/Views/Shared/_UploadsPartial.cshtml",uploadsModel);
        }

Hope it helps

OTHER TIPS

I dont know if this is the best way but here it goes. This works if you do not want to post back your current page.

@using (Html.BeginForm("UploadImage", "User", FormMethod.Post,
    new
    {
        enctype = "multipart/form-data",
        id = "ImgForm",
        name = "ImgForm",
        target = "UploadTarget",
        style = "display:none;"
    }))
{
    <h3>Upload Image</h3>
    <input type="file" name="ImageFile" id="ImageFile" />

}
<iframe id="UploadTarget" name="UploadTarget" style="position: absolute; left: -999em; top: -999em;"></iframe>


public void UploadImage(HttpPostedFileBase imageFile)
{
    if (imageFile != null)
    {
        byte[] logo = new byte[imageFile.ContentLength];
        imageFile.InputStream.Read(logo, 0, imageFile.ContentLength);
        ...
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top