I am calling a webmethod from my aspx page using ajax call. This webmethod is uploading files and I need the returned object to be of Type Object. Now in the below way the process is working and uploading files successfully, but my gridView could not be filled because the returned response is of type XMLDocument:
var fileUpload = $("#FileUpload1").get(0);
var files = fileUpload.files;
var fileData = new FormData();
fileData.append(files[0].name, files[0]);
fileData.append('username', 'Test');
$.ajax({
url: 'MyWebservice.asmx/UploadFiles',
type: "POST",
contentType: false,
processData: false,
data: fileData,
success: function (response) {
alert (response);
var gridView = $find('<%= grdvUploadFiles.ClientID %>');
gridView.set_dataSource(response);
gridView.dataBind();
}
});
In other aspx pages I am calling my webservice and filling gridViews successfully because the returned response is of type Object. Below is an example:
MyWebservice.MyWebMethod("test",LunchMyFunction);
function LunchMyFunction(response) {
alert(response);
var gridView = $find('<%= MyGridView.ClientID %>');
gridView.set_dataSource(response);
gridView.dataBind();
}
Now I have found a solution by using Json:
$.ajax({
type: "POST",
url: 'AutoComplete.asmx/UploadFiles',
data: JSON.stringify(fileData),
contentType: "application/json; charset=utf-8",
processData: false,
dataType: "json",
success: function (response) {
alert(response);
....
});
Now the returned response is of type Object (Which is perfect) BUT my webmethod could not catch the sent file, in other words: I haven't change my webmethod , but files counter :
HttpContext.Current.Request.Files.Count
is 0, while in the old ajax method (the first one mentionend) , the request count is returning 1 and file is uploaded successfully. Where is my problem? How can I send my file properly and return an object?
What I have tried:
I have tried also to add
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
to my webmethod but when I want to call from ajax an "internal server Error" is thrown