Forums - C# Corner

Forum guidelines
chetan shinde

chetan shinde

  • 1.4k
  • 59
  • 8.3k

Save image using ajax method

Sep 13 2017 2:06 AM
hi all.
 i am trying to save image using ajax call. but it didnt redirect to my web method. or i cant recieve image into webmethod.
here is my code.
 
function fileupload(name, fName) {
//var obj = {};
var obj = {};
var fl = new FormData();
var files = '';
if (name == "P")
files = $("#imgUploadPromition").get(0).files;
else
files = $("#fileUploadLogo").get(0).files;
var fileData = files[0];
// Add the uploaded image content to the form data collection
if (files.length > 0) {
//data.append("UploadedImage", files[0]);
fl.append(fName, (fileData));
//obj.append("fileName", fName);
}
else {
alert('Please Select File');
return;
obj.lst_to = "test";
obj.file = fl;
 
$.ajax({
type: 'POST',
url: "Content_New.aspx/UploadFiles",
data: JSON.stringify(obj),
cache: false,
processData: false,
contentType: false,
dataType: "json",
success: function (data) {
alert("Success!");
}
});
}
 
 
[WebMethod(EnableSession = true)]
public static string UploadFiles(string lst_to)
{
HttpPostedFile file2 = null;
if (HttpContext.Current.Request.Files.Count != 0)
file2 = HttpContext.Current.Request.Files[0];
Content_New objContent_New = new Content_New();
objContent_New.UploadFilePromotions(file2);
return "1";
}
 
Please help me for the same.  
 

Answers (2)