Upload Image with Web Service using jQuery AJAX in ASP.Net
[WebService(Namespace = "http://medicelo.in/")]
[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 Upload_Prescription : System.Web.Services.WebService
{
private bool _result;
public bool Result
{
get { return this._result; }
set { this._result = value; }
}
private string _message;
public string Message
{
get { return this._message; }
set { this._message = value; }
}
public Upload_Prescription()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string UploadPrescription(FileData fileData)
{
Result = false;
ShowMessage msg = new ShowMessage();
if (!string.IsNullOrEmpty(fileData.FileName))
{
try
{
byte[] imageBytes = Convert.FromBase64String(fileData.Data);
string filePath = "https://medicelo.in/assests/prescription/" + fileData.FileName.Replace(" ", "_");
System.IO.File.WriteAllBytes(Server.MapPath(filePath), imageBytes);
using (SqlConnection conn = Connection.getConnection())
{
using (SqlCommand cmd = new SqlCommand("usp_insert_tbl_customer_prescription", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@mobile", fileData.Mobile);
cmd.Parameters.AddWithValue("@prescription", fileData.FileName);
cmd.Parameters.AddWithValue("@pimagepaht", filePath);
cmd.Parameters.AddWithValue("@vdate", System.DateTime.Now);
cmd.Parameters.AddWithValue("@remarks", fileData.Remark);
cmd.Parameters.AddWithValue("@makeorder", fileData.MakeOrder);
cmd.Parameters.AddWithValue("@ordercount", fileData.OrderCount);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Result = true;
Message = "Congratulation ! Prescription Upload Successfully";
}
}
}
catch (Exception ex)
{
Message = ex.Message;
}
}
msg.Message = Message.ToString();
msg.Result = Result.ToString();
return JsonConvert.SerializeObject(msg);
}
public class FileData
{
public string Mobile { get; set; }
public string FileName { get; set; }
public string Data { get; set; }
public string Remark { get; set; }
public string MakeOrder { get; set; }
public string OrderCount { get; set; }
}
public class ShowMessage
{
public string Message { get; set; }
public string Result { get; set; }
}
}
i also setting in webconfig file but it not working.
<system.webServer>
<httpProtocol>
<customHeaders>
<!-- Adding the following custom HttpHeader will help prevent CORS from stopping the Request-->
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
$.ajax({
type: "POST",
url: "https://medicelo.in/Upload_Prescription.asmx/UploadPrescription",
data: '{fileData : ' + JSON.stringify(obj) + ' }',
contentType: "application/json; charset=utf-8",
dataType: "json",
crossDomain: true,
success: function (response) {
var data = response.d;
data = $.parseJSON(data);
if (data.Result == 'True') {
$.iaoAlert({ position: 'top-right', msg: data.Message, type: "success", mode: "dark", });
window.location.href = "customerprescription.aspx";
}
else {
$.iaoAlert({ position: 'top-right', msg: data.Message, type: "error", mode: "dark", });
}
var output = document.getElementById('preview');
output.src = "assests/images/Upload-icon.png";
$('.btn-outline-secondary').hide();
},
error: function (r) {
alert(r.responseText);
},
failure: function (r) {
alert(r.responseText);
}
});
return false;
});