File Upload Validation Using jQuery

Hi, In this blog we will see client side validation for asp:FileUpload control. Many times we need to upload files in SharePoint list/library using visual web part. If we want to check file size of selected files then you can use below code.

In this blog we will see client side validation for asp:FileUpload control. Many times we need to upload files in SharePoint list/library using visual web part. If we want to check file size of selected files then you can use below code.

If we directly use asp:FileUpload then it will allow you to select files from some specific location. If we used jquery file upload then it allows user to select files from different locations.

Below are the jquery files that are required. Download it from library and save it in location from where you can give reference.
  1. <script type="text/javascript" src="/Documents/Project/jquery-1.9.1.min.js"></script>  
  2. <script type="text/javascript" src="/Documents/Project/jquery.MultiFile.js"></script>  
I have careted one visual web part and in that I have added "asp:FileUpload" control. I have added CssClass="multi" to file control for jquery functionality.

Below is the HTML For the same. Currently I have checked for 2 MB size.
  1. <div class="form-group" id="Attachment">  
  2.     <div class="col-sm-2"> <label for="Attachment">Attachments:</label> </div>  
  3.     <div class="col-sm-4">  
  4.         <asp:FileUpload ID="fuITRequestAttachment" CssClass="multi" runat="server" /> <span>Max file size is 2MB</span> </div>  
  5.     <div class="col-sm-4" id="fileAttachments" runat="server"> </div>  
  6. </div>  
  7. <asp:Button ID="btnSave" runat="server" CssClass="btn btn-primary" Text="Submit" OnClientClick="javascript:return validate();" OnClick="btnSave_Click1" />  
Validate function will get executed when we click on Submit button. I have written code to check file size in validate function.

Whenever we upload file in file upload control (with jquery multifile) then input:file control gets created dynamically at run time based on number of files selected.

So in the below code I have taken all input : file control and checked size of files. For now I have used file size 2 MB.
  1. function validate() {  
  2.     var validate = false;  
  3.     $('.MultiFile-wrap').each(function() {  
  4.         var file = $("input:file");  
  5.         if (file.length > 1) {  
  6.             for (var i = 0; i < file.length; i++) {  
  7.                 var x = $("#[id=" + file[i].id + "]");  
  8.                 if (x != undefined) {  
  9.                     if (x[0].files != undefined) {  
  10.                         if (x[0].files[0].size > 2097152) {  
  11.                             alert('Please select file size less than 2 MB');  
  12.                             validate = false;  
  13.                             break;  
  14.                         }  
  15.                     }  
  16.                 }  
  17.             }  
  18.         }  
  19.     });  
  20.     return validate;  
  21. }  
After size validation I have saved all files in SharePoint list item attachment. Below is the code for that
open .ascx.cs file and add below code on button click event
  1. protected void btnSave_Click1(object sender, EventArgs e) {  
  2.     try {  
  3.         SPSite site = new SPSite(SPContext.Current.Web.Url);  
  4.         using(SPWeb web = site.OpenWeb()) {  
  5.             SPList lst = web.Lists.TryGetList("ListName");  
  6.             SPListItem item = lst.Items.Add();  
  7.             item["Title"] = "File upload";  
  8.             if (HttpContext.Current.Request.Files.Count > 0) {  
  9.                 SPAttachmentCollection attach = item.Attachments;  
  10.                 for (var i = 0; i < HttpContext.Current.Request.Files.Count; i++) {  
  11.                     HttpPostedFile file = HttpContext.Current.Request.Files[i] as HttpPostedFile;  
  12.                     string strFileName = file.FileName;  
  13.                     if (!string.IsNullOrEmpty(strFileName)) {  
  14.                         Stream fStream = file.InputStream;  
  15.                         byte[] binFile = new byte[file.ContentLength];  
  16.                         fStream.Read(binFile, 0, binFile.Length);  
  17.                         attach.Add(strFileName, binFile);  
  18.                         fStream.Close();  
  19.                         fStream.Dispose();  
  20.                     }  
  21.                 }  
  22.             }  
  23.             item.Update();  
  24.         }  
  25.     } catch (Exception ex) {  
  26.         throw;  
  27.     }  
  28. }  
Build and deploy above web part and check validation for file size.