Ad

Search This Blog

Friday, July 3, 2015

How to Upload a file in MVC4


In View:-

@using (Html.BeginForm("SaveUploadDocuments", "UploadDocuments", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()
        <div>
            <table >
                <tr>
                    <td colspan="2"></td>
                </tr>
                <tr>
                    <td colspan="2"></td>
                </tr>
                <tr>
                    <td>
                        <table>
                            <tr>
                                <td class="tdLabel" align="left">
                                    Select file to Upload
                                </td>
                                <td align="left">
                                    <input type="file" name="UploadFile" style="background-color:whitesmoke;border:2px solid;border-color:lightgray; width:220px;" />
                                </td>
                                <td align="left">
                                    <input type="submit" value="Upload" class="button" />
                                </td>
                                <td align="left">
                                    <input type="submit" value="Close" class="button" onclick="ClosePopup();" />
                                </td>
                            </tr>
                        </table>
                    </td>
                    
                    
                </tr>
                <tr>
                     
                    <td align="left">
                        @{
                            string message = TempData["Message"] as string;
                            if (TempData["Error"] != null)
                            {
                                <div class="red">
                                    <p style="color:red;font-size:12px; font-family:Calibri;"><strong style="color:red;font-size:12px; font-family:Calibri;">Error:</strong> @TempData["Error"].ToString()</p>
                                </div>
                            }
                            if (TempData["Success"] != null)
                            {
                                <div class="green">
                                    <p style="color:blue;font-size:12px; font-family:Calibri;"><strong style="color:blue;font-size:12px; font-family:Calibri;">Success:</strong> @TempData["Success"].ToString()</p>
                                </div>
                            }

                        }
                    </td>
                </tr>
                
                 
            </table>


        </div>
    }


In Controller :-

 [HttpPost]
        public ActionResult SaveUploadDocuments(FormCollection formCollection)
        {
            var fileLicenseUpload = new FileUpload();
            HttpPostedFileBase file = Request.Files["UploadFile"];
            if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
            {
                if (file == null)
                {
                    ModelState.AddModelError("File", "Please Upload Your file");
                }
                else if (file.ContentLength > 0)
                {
                    using (var db1 = new SupplierContext())
                    {
                        fileLicenseUpload.UserId = Convert.ToInt32(Session["EditSupplierId"].ToString().Trim());
                        fileLicenseUpload.FileName = Path.GetFileName(file.FileName);
                        fileLicenseUpload.FileContentType = file.ContentType;
                        fileLicenseUpload.FileData = new Byte[file.ContentLength];
                        file.InputStream.Read(fileLicenseUpload.FileData, 0, Convert.ToInt32(file.ContentLength));
                        fileLicenseUpload.FileUploadDate = System.DateTime.Now;
                        db1.FileUpload.Add(fileLicenseUpload);
                        db1.SaveChanges();
                        TempData["Success"] = "Document uploaded successfully." + " " + file.FileName.ToString().Trim();
                    }
                    ModelState.Clear();
                }
            }
            return View("UploadDocuments");
        }

No comments:

Post a Comment