views:

147

answers:

1

I'm having a problem similar to http://stackoverflow.com/questions/2721970/jquery-ajax-not-working-in-ie8-but-it-works-on-firefox-chrome, but with a different use case.

I'm using the jQuery Form plug-in to handle a file upload to an ASP.NET MVC controller, which sends the file off for parsing and processing. If an Exception is thrown, it should alert the user to the issue.

//client side code
//make an ajax call, sending the contents of the file
                    $("#ajaxUploadForm").ajaxSubmit({
                        dataType: 'json',
                        url: '/plan/Something/ExcelImport',
                        iframe: true,
                        beforeSend: function () {
                            $(".ui-dialog-buttonpane").find("#my_progress").fadeIn();
                        },
                        success: function (data, textStatus) {
                            output = "<center><span class='flash'>" + data.message + "</span></center>";
                            $("#flash_message").html(output).fadeIn('slow');
                            setTimeout(function () { $("#flash_message").fadeOut() }, 5000);
                            cleanup();
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert("XMLHttpRequest is " + XMLHttpRequest);
                            var contents = "";
                            for (prop in XMLHttpRequest) {
                                contents += "\na property is " + prop + " it's value is " + XMLHttpRequest[prop];
                            }
                            alert("the contents are " + contents);
                            alert("textStatus is " + textStatus);
                            alert("errorThrown is " + errorThrown);
                            //comes back in an HTML envelope. This should be parsed with regex, but I can't get it to work. Dirty hack
                            response = XMLHttpRequest.responseText.substring(XMLHttpRequest.responseText.indexOf("<body>"));
                            response = response.replace("<body>", "");
                            response = response.replace("</body>", "");
                            alert("There was a problem with the upload.\r\n" + response);
                        },
                        complete: function (XMLHttpRequest, textStatus) {
                            $(".ui-dialog-buttonpane").find("#my_progress").remove();
                            something_import.dialog('close');
                            something_import.dialog('destroy');
                        }
                    });

//server side code
public FileUploadJsonResult ExcelImport()
    {
        FileUploadJsonResult result = new FileUploadJsonResult();
        HttpPostedFileBase hpf = Request.Files[0] as HttpPostedFileBase;
        if (hpf.ContentLength == 0)
            return new FileUploadJsonResult { Data = new { message = "File contained no data" } };
        String fileName = Path.GetFileName(hpf.FileName);
        String timeStampedFile = fileName.Insert(fileName.IndexOf('.'),"_"+DateTime.Now.ToFileTimeUtc());
        string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "tempo", timeStampedFile);
        hpf.SaveAs(savedFileName);
        try
        {
            result = ProcessFile(savedFileName, Request["Id"]) as FileUploadJsonResult;
        }
        catch (ArgumentException e)
        {
            this.Response.StatusCode = 500;
            this.Response.StatusDescription = System.Net.HttpStatusCode.BadRequest.ToString();
            Response.Write(e.Message);
            result = Json(new { message = e.Message, stackTrace = e.StackTrace }) as FileUploadJsonResult;  
        }
        return result;
    }

This works perfectly in Chrome and Firefox. In IE, the XMLHttpRequest object coming back is different:

FF:alt text

IE: alt text

I've been Googling around for differences between the browser implementations of XMLHttpRequest, but haven't found anything that deals specifically with this case. Stymied.

+2  A: 

The wikipedia article on XMLHttpRequest seems to give a good overview of the history behind the XMLHttpRequest. It seems Microsoft and Mozilla developed/adopted their own versions of the object and hence why you are probably seeing different properties.

Here is a link to Microsoft's implementation of the XMLHttpRequest interface members, which seem to match the properties in your alert.

Here is the a link to Mozilla's implementation of XMLHttpRequest.

So while we wait for the W3C to standardize the XMLHttpRequest you will continue to have different implementations across the browsers like you are seeing in this case.

For some added fun here is Apple's and Opera's specifications on XMLHttpRequest.

amurra