0

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

Max
  • 1,289
  • 3
  • 26
  • 50
  • I'm presuming this is an upload from a web-page via an ajax call? The jQuery ajax code might be useful. Also, if you use something like the network tab of chrome to see the request/response, what is the actual content type returned from the request (if you look the in the response headers) and is the data appearing in the body of the response properly? – Klors May 22 '13 at 18:31
  • Alternatively, if you're posting the page from a form you may be having this issue http://stackoverflow.com/questions/9230779/ie9-prompts-user-on-submission-of-hidden-iframe – Klors May 22 '13 at 19:04
  • that issue does not happen with Chrome. The data returned (JSON structure) is appearing in the response HTTP 200. – Max May 22 '13 at 19:10
  • Klors: sorry just your link. That did the job. Thank you! Not sure where or how I mark your answer as good and final. – Max May 22 '13 at 20:19
  • Glad it helped, I'll put is as an answer so you can mark it. – Klors May 22 '13 at 22:49

1 Answers1

0

The browser is not understanding the returned data and so is prompting you to save it as it doesn't know what else to do with it.

The information in this question should help. IE9 prompts user on submission of hidden iFrame

Community
  • 1
  • 1
Klors
  • 2,665
  • 2
  • 25
  • 42