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.Save uploaded image path in Database using ASMX WebService and jQuery Ajax in ASP.Net<p>Hi&nbsp;MehmetBil,</p> <p>Refer below&nbsp;example and&nbsp;correct your code as per your requirement.</p> <p><strong><span style="text-decoration: underline;">SQL</span></strong></p> <pre class="brush: sql">CREATE TABLE [dbo].[tblFiles_Path] ( [Id] [varchar](50) NOT NULL, [Name] [varchar](50) NULL, [Path] [varchar](50) NULL )</pre> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;asp:FileUpload ID=&#34;FileUpload1&#34; runat=&#34;server&#34; /&gt; &lt;asp:Button ID=&#34;btnSave&#34; Text=&#34;Save&#34; runat=&#34;server&#34; /&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { var reader = new FileReader(); var fileName; $(&#39;[id*=FileUpload1]&#39;).change(function () { if (typeof (FileReader) != &#34;undefined&#34;) { var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/; $($(this)[0].files).each(function () { var file = $(this); if (regex.test(file[0].name.toLowerCase())) { fileName = file[0].name; reader.readAsDataURL(file[0]); } else { alert(file[0].name + &#34; is not a valid image file.&#34;); return false; } }); } else { alert(&#34;This browser does not support HTML5 FileReader.&#34;); } }); $(&#34;[id*=btnSave]&#34;).click(function () { var byteData = reader.result; byteData = byteData.split(&#39;;&#39;)[1].replace(&#34;base64,&#34;, &#34;&#34;); var obj = {}; obj.Data = byteData; obj.Name = fileName; $.ajax({ type: &#34;POST&#34;, url: &#34;ImageService.asmx/SaveImage&#34;, data: &#39;{fileData : &#39; + JSON.stringify(obj) + &#39; }&#39;, contentType: &#34;application/json; charset=utf-8&#34;, dataType: &#34;json&#34;, success: function (r) { }, error: function (r) { alert(r.responseText); }, failure: function (r) { alert(r.responseText); } }); return false; }); }); &lt;/script&gt;</pre> <p><strong><span style="text-decoration: underline;">ImageService</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System; using System.Collections.Generic; using System.Configuration; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.Services; /// &lt;summary&gt; /// Summary description for ImageService /// &lt;/summary&gt; [WebService(Namespace = &#34;http://tempuri.org/&#34;)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class ImageService : System.Web.Services.WebService { public ImageService() { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public void SaveImage(FileData fileData) { if (!string.IsNullOrEmpty(fileData.Name)) { byte[] imageBytes = Convert.FromBase64String(fileData.Data); string filePath = &#34;~/Files/&#34; + fileData.Name; System.IO.File.WriteAllBytes(Server.MapPath(filePath), imageBytes); string constr = ConfigurationManager.ConnectionStrings[&#34;conString&#34;].ConnectionString; using (SqlConnection conn = new SqlConnection(constr)) { string sql = &#34;INSERT INTO tblFiles_Path VALUES(NEWID(),@Name, @Path)&#34;; using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddWithValue(&#34;@Name&#34;, fileData.Name); cmd.Parameters.AddWithValue(&#34;@Path&#34;, filePath); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } } } public class FileData { public string Name { get; set; } public string Data { get; set; } } } </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.Web Imports System.Web.Services Imports System.Web.Services.Protocols &#39; To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. &lt;System.Web.Script.Services.ScriptService()&gt; &lt;WebService(Namespace:=&#34;http://tempuri.org/&#34;)&gt; &lt;WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)&gt; &lt;Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()&gt; Public Class ImageService Inherits System.Web.Services.WebService &lt;WebMethod&gt; Public Sub SaveImage(ByVal fileData As FileData) If Not String.IsNullOrEmpty(fileData.Name) Then Dim imageBytes As Byte() = Convert.FromBase64String(fileData.Data) Dim filePath As String = &#34;~/Files/&#34; &amp; fileData.Name System.IO.File.WriteAllBytes(Server.MapPath(filePath), imageBytes) Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;conString&#34;).ConnectionString Using conn As SqlConnection = New SqlConnection(constr) Dim sql As String = &#34;INSERT INTO tblFiles_Path VALUES(NEWID(),@Name, @Path)&#34; Using cmd As SqlCommand = New SqlCommand(sql, conn) cmd.Parameters.AddWithValue(&#34;@Name&#34;, fileData.Name) cmd.Parameters.AddWithValue(&#34;@Path&#34;, filePath) conn.Open() cmd.ExecuteNonQuery() conn.Close() End Using End Using End If End Sub Public Class FileData Public Property Name As String Public Property Data As String End Class End Class</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/719516/Save-uploaded-image-path-in-Database-using-ASMX-WebService-and-jQuery-Ajax-in-ASPNet/https://www.aspforums.net:443/Threads/719516/Save-uploaded-image-path-in-Database-using-ASMX-WebService-and-jQuery-Ajax-in-ASPNet/Mon, 10 Sep 2018 03:56:43 GMT