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 File in Folder (Directory) and path in Database using jQuery Ajax and Generic Handler in ASP.Net<p>Hi <a class="username" rel="SajidHussa"> SajidHussa</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;&lt;/title&gt; &lt;style type=&#34;text/css&#34;&gt; body { font-family: Arial; font-size: 10pt; } #dvPreview { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image); min-height: 450px; min-width: 450px; display: none; } img { height: 350px; width: 350px; } &lt;/style&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script language=&#34;javascript&#34; type=&#34;text/javascript&#34;&gt; $(function () { $(&#34;#fileupload&#34;).change(function () { $(&#34;#dvPreview&#34;).html(&#34;&#34;); var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/; if (regex.test($(this).val().toLowerCase())) { if ($.browser.msie &amp;&amp; parseFloat(jQuery.browser.version) &lt;= 9.0) { $(&#34;#dvPreview&#34;).show(); $(&#34;#dvPreview&#34;)[0].filters.item(&#34;DXImageTransform.Microsoft.AlphaImageLoader&#34;).src = $(this).val(); } else { if (typeof (FileReader) != &#34;undefined&#34;) { $(&#34;#dvPreview&#34;).show(); $(&#34;#dvPreview&#34;).append(&#34;&lt;img /&gt;&#34;); var reader = new FileReader(); reader.onload = function (e) { $(&#34;#dvPreview img&#34;).attr(&#34;src&#34;, e.target.result); } reader.readAsDataURL($(this)[0].files[0]); var img = $(&#34;#fileupload&#34;).get(0); var files = img.files; var data = new FormData(); for (var i = 0; i &lt; files.length; i++) { data.append(files[i].name, files[i]); } $.ajax({ type: &#39;POST&#39;, data: data, url: &#34;Handler.ashx&#34;, cache: false, contentType: false, processData: false, success: function (r) { alert(r.d); }, error: function (r) { alert(r.d); } }); } else { alert(&#34;This browser does not support FileReader.&#34;); } } } else { alert(&#34;Please upload a valid image file.&#34;); } }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;div&gt; &lt;input id=&#34;fileupload&#34; type=&#34;file&#34; /&gt; &lt;hr /&gt; &lt;b&gt;Live Preview&lt;/b&gt;&lt;br /&gt;&lt;br /&gt; &lt;div id=&#34;dvPreview&#34;&gt;&lt;/div&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <p><strong>HandlerCS.ashx</strong></p> <pre class="brush: csharp">public void ProcessRequest(HttpContext context) { if (context.Request.Files.Count &gt; 0) { HttpPostedFile postedFile = context.Request.Files[0]; string folderPath = context.Server.MapPath(&#34;~/Uploads/&#34;); string fileName = System.IO.Path.GetFileName(postedFile.FileName); postedFile.SaveAs(folderPath + fileName); string filPath = folderPath + fileName; string constr = System.Configuration.ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(constr)) { using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(&#34;INSERT INTO tblfilePath VALUE(@Path)&#34;, con)) { cmd.Parameters.AddWithValue(&#34;@Path&#34;, filPath); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } } } public bool IsReusable { get { return false; } }</pre> <p><strong>VB.Net</strong></p> <p>HandlerVB.ashx</p> <pre class="brush: vb">Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest If context.Request.Files.Count &gt; 0 Then Dim postedFile As HttpPostedFile = context.Request.Files(0) Dim folderPath As String = context.Server.MapPath(&#34;~/Uploads/&#34;) Dim fileName As String = System.IO.Path.GetFileName(postedFile.FileName) postedFile.SaveAs(folderPath &amp; fileName) Dim filPath As String = folderPath &amp; fileName Dim constr As String = System.Configuration.ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(constr) Using cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(&#34;INSERT INTO tblfilePath VALUE(@Path)&#34;, con) cmd.Parameters.AddWithValue(&#34;@Path&#34;, filPath) con.Open() cmd.ExecuteNonQuery() con.Close() End Using End Using End If End Sub Public ReadOnly Property IsReusable As Boolean Get Return False End Get End Property</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/2oTyBbh.gif" alt="" width="367" height="482" /></p>https://www.aspforums.net:443/Threads/236341/Upload-File-in-Folder-Directory-and-path-in-Database-using-jQuery-Ajax-and-Generic-Handler-in-ASPNet/https://www.aspforums.net:443/Threads/236341/Upload-File-in-Folder-Directory-and-path-in-Database-using-jQuery-Ajax-and-Generic-Handler-in-ASPNet/Mon, 01 Jul 2019 04:17:45 GMT