Posted by : Jebastin Tuesday 21 January 2014

The following AJAX File Upload jQuery Tutorial covers how to upload files asynchronously using jQuery Framework.

Generic Handler: AjaxFileUploader.ashx

  1. public class AjaxFileUploader : IHttpHandler
  2. {
  3. public void ProcessRequest(HttpContext context)
  4. {
  5.     if (context.Request.Files.Count > 0)
  6.     {
  7.         string path = context.Server.MapPath("~/Files");
  8.         if (!Directory.Exists(path))
  9.             Directory.CreateDirectory(path);
  10.         var file = context.Request.Files[0];
  11.         string fileName;
  12.         if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")
  13.         {
  14.             string[] files = file.FileName.Split(new char[] { '\\' });
  15.             fileName = files[files.Length - 1];
  16.         }
  17.         else
  18.         {
  19.             fileName = file.FileName;
  20.         }
  21.         string strFileName = fileName;
  22.         fileName = Path.Combine(path, fileName);
  23.         file.SaveAs(fileName);
  24.         string msg = "{";
  25.         msg += string.Format("error:'{0}',\n", string.Empty);
  26.         msg += string.Format("msg:'{0}',\n", strFileName);
  27.         msg += string.Format("path:'{0}'", path.Replace("\\", "?"));
  28.         msg += "}";
  29.         context.Response.Write(msg);
  30.     }
  31. }
  32. public bool IsReusable
  33. {
  34.     get
  35.     {
  36.         return true;
  37.     }
  38. }
  39. }

HTML:

  1. <div class="resumeUpload">
  2.     <input type="file" id="fileToUpload" name="fileToUpload" onchange="ajaxFileUpload();" />
  3.     <div class="fileBtn">
  4.         <span>File</span>
  5.         <input id="fileUploadBox" disabled="disabled" value="@GetDictionary("No files selected")" />
  6.     </div>
  7. </div>

jQuery:

  1. <script type="text/javascript">
  2. function ajaxFileUpload() {
  3.     $("#fileToUpload").ajaxStart(function () {
  4.         $('#fileUploadBox').val('Uploading...');
  5.         $('#fileUploadBox').addClass('loadinggif');
  6.     }).ajaxComplete(function () {
  7.         $('#fileUploadBox').removeClass('loadinggif');
  8.     });
  9.     $.ajaxFileUpload({
  10.         url: '/GenericHandlers/AjaxFileUploader.ashx',
  11.         secureuri: false,
  12.         fileElementId: 'fileToUpload',
  13.         dataType: 'json',
  14.         data: { name: 'logan', id: 'id' },
  15.         success: function (data, status) {
  16.             if (typeof (data.error) != 'undefined') {
  17.                 if (data.error != '') {
  18.                     //alert(data.error);
  19.                 } else {
  20.                     $('#fileUploadBox').val(data.msg);
  21.                     $("#hdnFilePath").val(data.path + "?" + data.msg);
  22.                 }
  23.             }
  24.         },
  25.         error: function (data, status, e) {
  26.             //alert(e);
  27.             $('#fileUploadBox').val('File size is too large.');
  28.         }
  29.     });
  30.     $.extend({
  31.         handleError: function (s, xhr, status, e) {
  32.             if (s.error)
  33.                 s.error(xhr, status, e);
  34.             else if (xhr.responseText)
  35.                 console.log(xhr.responseText);
  36.         }
  37.     });
  38.     return false;
  39. }
  40. </script>
Source: Hayageek

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Link To This Post/Page

Spread The Word

Add this button to your blog:
JJ Technology Solutions

Blog Archive

Trackers

eXTReMe Tracker
facebook

- Copyright © JJ Technology Solutions - Powered by Source Code Solutions -