ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Upload and save File in Folder (Directory) using AJAX FormData and Generic Handler in ASP.Net<p>Hi <a class="username" rel="nauna"> nauna</a>,</p> <p>Use ResolveUrl to set the path.</p> <p>Refer the below code.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;input type=&#34;file&#34; name=&#34;postedFile&#34; /&gt; &lt;input type=&#34;button&#34; id=&#34;btnUpload&#34; value=&#34;Upload&#34; /&gt; &lt;progress id=&#34;fileProgress&#34; style=&#34;display: none&#34;&gt;&lt;/progress&gt; &lt;hr /&gt; &lt;span id=&#34;lblMessage&#34; style=&#34;color: Green&#34;&gt;&lt;/span&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(&#34;body&#34;).on(&#34;click&#34;, &#34;#btnUpload&#34;, function () { $.ajax({ url: &#39;&lt;%= ResolveUrl(&#34;~/HandlerCS.ashx&#34;)%&gt;&#39;, type: &#39;POST&#39;, data: new FormData($(&#39;form&#39;)[0]), cache: false, contentType: false, processData: false, success: function (file) { $(&#34;#fileProgress&#34;).hide(); $(&#34;#lblMessage&#34;).html(&#34;&lt;b&gt;&#34; + file.name + &#34;&lt;/b&gt; has been uploaded.&#34;); }, xhr: function () { var fileXhr = $.ajaxSettings.xhr(); if (fileXhr.upload) { $(&#34;progress&#34;).show(); fileXhr.upload.addEventListener(&#34;progress&#34;, function (e) { if (e.lengthComputable) { $(&#34;#fileProgress&#34;).attr({ value: e.loaded, max: e.total }); } }, false); } return fileXhr; } }); }); &lt;/script&gt;</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/176665/Upload-and-save-File-in-Folder-Directory-using-AJAX-FormData-and-Generic-Handler-in-ASPNet/https://www.aspforums.net:443/Threads/176665/Upload-and-save-File-in-Folder-Directory-using-AJAX-FormData-and-Generic-Handler-in-ASPNet/Thu, 05 Sep 2019 05:10:43 GMTUpload and save File in Folder (Directory) using AJAX FormData and Generic Handler in ASP.Net<p>Hi <a class="username" rel="nauna"> nauna</a>,</p> <p>Refer the sample code.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <p><span style="text-decoration: underline;">feed.aspx</span></p> <pre class="brush: html">&lt;input type=&#34;file&#34; name=&#34;postedFile&#34; /&gt; &lt;input type=&#34;button&#34; id=&#34;btnUpload&#34; value=&#34;Upload&#34; /&gt; &lt;progress id=&#34;fileProgress&#34; style=&#34;display: none&#34;&gt;&lt;/progress&gt; &lt;hr /&gt; &lt;span id=&#34;lblMessage&#34; style=&#34;color: Green&#34;&gt;&lt;/span&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(&#34;body&#34;).on(&#34;click&#34;, &#34;#btnUpload&#34;, function () { $.ajax({ url: &#39;&lt;%= ResolveUrl(&#34;~/HandlerCS.ashx&#34;)%&gt;&#39;, type: &#39;POST&#39;, data: new FormData($(&#39;form&#39;)[0]), cache: false, contentType: false, processData: false, success: function (file) { $(&#34;#fileProgress&#34;).hide(); $(&#34;#lblMessage&#34;).html(&#34;&lt;b&gt;&#34; + file.name + &#34;&lt;/b&gt; has been uploaded.&#34;); }, xhr: function () { var fileXhr = $.ajaxSettings.xhr(); if (fileXhr.upload) { $(&#34;progress&#34;).show(); fileXhr.upload.addEventListener(&#34;progress&#34;, function (e) { if (e.lengthComputable) { $(&#34;#fileProgress&#34;).attr({ value: e.loaded, max: e.total }); } }, false); } return fileXhr; } }); }); &lt;/script&gt;</pre> <p><strong><span style="text-decoration: underline;">Handler</span></strong></p> <pre class="brush: csharp">&lt;%@ WebHandler Language=&#34;C#&#34; Class=&#34;HandlerCS&#34; %&gt; using System; using System.IO; using System.Net; using System.Web; using System.Web.Script.Serialization; public class HandlerCS : IHttpHandler { public void ProcessRequest(HttpContext context) { //Check if Request is to Upload the File. if (context.Request.Files.Count &gt; 0) { //Fetch the Uploaded File. HttpPostedFile postedFile = context.Request.Files[0]; //Set the Folder Path. string folderPath = context.Server.MapPath(&#34;~/Uploads/&#34;); //Set the File Name. string fileName = Path.GetFileName(postedFile.FileName); //Save the File in Folder. postedFile.SaveAs(folderPath + fileName); //Send File details in a JSON Response. string json = new JavaScriptSerializer().Serialize(new { name = fileName }); context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.ContentType = &#34;text/json&#34;; context.Response.Write(json); context.Response.End(); } } public bool IsReusable { get { return false; } } }</pre> <p>Page is inside news folder which is inside the url folder.</p> <p>Url will be ../user/feed.aspx</p>https://www.aspforums.net:443/Threads/176665/Upload-and-save-File-in-Folder-Directory-using-AJAX-FormData-and-Generic-Handler-in-ASPNet/https://www.aspforums.net:443/Threads/176665/Upload-and-save-File-in-Folder-Directory-using-AJAX-FormData-and-Generic-Handler-in-ASPNet/Thu, 05 Sep 2019 08:10:10 GMT