Skip to content
This repository has been archived by the owner on May 25, 2023. It is now read-only.

When sending JSON back via an iframe transport, IE prompts for download #1795

Closed
phallguy opened this issue Oct 22, 2012 · 9 comments
Closed

Comments

@phallguy
Copy link

When using the iframe transport fallback for IE browsers, and responding with a JSON object and content type of 'application/json', Internet Explorer will prompt to download the file. Responding with content type 'text/html' resolves the problem, however it is not exactly correct and not something that should be returned for XHR requests or REST clients. I'd like to propose adding a field to the data submitted via iframe transport so that the server can identify those requests and adjust the content type dynamically.

I simply added the following immediately before the bit that copies options.formData (~line 93)

form.append('')

@nosykretts
Copy link

#659 (comment)

@phallguy
Copy link
Author

@nosykretts Right, setting a response type of 'text/html' will resolve the problem. But it is incorrect to return JSON data as content type text/html for REST clients and browsers that work correctly. The flag would allow server side logic to only use the text/html hack for incompatible IE clients.

@blueimp
Copy link
Owner

blueimp commented Oct 23, 2012

From the Setup guide:

Content-Type Negotiation

The file upload plugin makes use of an Iframe Transport module for browsers like Microsoft Internet Explorer and Opera, which do not yet support XMLHTTPRequest file uploads.
Iframe based uploads require a Content-type of text/plain or text/html for the JSON response - they will show an undesired download dialog if the iframe response is set to application/json.

You can make use of the Accept header to offer different content types for the file upload response. Here is the (PHP) example code snippet for the Accept content-type variation:

<?php
header('Vary: Accept');
if (isset($_SERVER['HTTP_ACCEPT']) &&
    (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)) {
    header('Content-type: application/json');
} else {
    header('Content-type: text/plain');
}
?>

@blueimp blueimp closed this as completed Oct 23, 2012
@Kaster
Copy link

Kaster commented Oct 23, 2012

For java applies equivalent:

    if (request.getHeader("accept").indexOf("application/json") != -1) {
        response.setContentType("application/json; charset=UTF-8");
    } else {
        // IE workaround
        response.setContentType("text/plain; charset=UTF-8");
    }

@saxenad
Copy link

saxenad commented Oct 30, 2012

can you tell me how to get around this in .net/IIS 7? i am not sure where to place the content type =text line

@alexandrejobin
Copy link

For MVC users under IIS, do it that way:

var headerAcceptVariable = this.Request.Headers["ACCEPT"] as string;

if (headerAcceptVariable != null && headerAcceptVariable.Contains("application/json"))
{
    return Json(myDataVariable);
}
else
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    var response = serializer.Serialize(myDataVariable);

    return Content(response);
}

@CanCeylan
Copy link

Can you give an example for rails app as well ?

@seyin
Copy link

seyin commented Dec 24, 2012

Yes, please, I'd like an example for rails too

@Burkazoid
Copy link

For a Rails example, see the Controller section here: https://github.com/blueimp/jQuery-File-Upload/wiki/Rails-setup-for-V6

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants