I am trying to return a JSON structure after a file has been uploaded. The file is saved in the web server just fine. The problem is when I try to return data using structure "UploadJobStatus", IE will prompt me to save the file: "Do you want to open or save UploadFile554b0687 from localhost?" The file is first saved in the web server, then I get the prompted question with the weird file name. If I save the file I see that the content is the returned JSON data:
[{"Error":"Uploaded file successfully.","Successful":true}]
However, if I change my interface to return just VOID, I don't get prompt. I need to return that structure so that I can loop through the returned data in my jQuery code.
Interface:
[System.Web.Services.WebMethod(EnableSession = true)]
[OperationContract(Name = "RunJob_UploadFile")]
[WebInvoke(UriTemplate = "/RunJob/UploadFile", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Method = "POST")]
List<RunJob.UploadJobStatus> UploadFile();
Code:
public List<UploadFileStatus> UploadFile()
{
List<UploadFileStatus> temp = new List<UploadFileStatus>();
var files = System.Web.HttpContext.Current.Request.Files;
if ((files.Count > 0) && (!String.IsNullOrEmpty(files[0].FileName)))
{
try
{
HttpContext.Current.Request.Files[0].SaveAs(@"C:\TEST\TEST.ZIP");
temp.Add(new UploadFileStatus(true, "Uploaded file successfully."));
return temp;
//return;
}
catch (Exception e)
{
temp.Add(new UploadFileStatus(false, "Not able to upload the file!"));
return temp;
}
...
Structure of List:
public struct UploadFileStatus
{
public bool Successful;
public string Error;
public UploadFileStatus(bool successful, string error)
{
this.Successful = successful;
this.Error = error;
}
}
jQuery:
function UploadFile() {
if ($("#FileToUpload").val() != "") {
$.ajaxFileUpload
(
{
url: "svc/RunJob.svc/RunJob/UploadFile",
secureuri: false,
fileElementId: "FileToUpload",
data:
{
},
success: function (jqXHR, textStatus) {
if (textStatus == "success") {
alert("File was uploaded.");
}
else {
alert("Upload file failed!");
}
},
error: function () {
alert("Error!");
}
}
)
}
}
Any ideas? Thank you