English 中文(简体)
Internet Explorer blocked file download; file of "Unknown File Type"
原标题:

I m developing a front-end to a Rails application. In cross-browser testing, I immediately discovered that Internet Explorer (apparently all modern versions, but at least IE 7 and IE 8) is not correctly interpreting a file I m trying to load via AJAX (with jQuery) as JavaScript. A file download warning appears and the user needs to confirm that the file should be downloaded. Unfortunately, this is not acceptable for the purposes of the application.

I created a couple of test files; one is just a JavaScript file served from Amazon S3; the other is actually a resource URL served by Varnish/Rails. The latter is the one that triggers the warning. So:


LINK: URL that gives a warning in IE

REQUEST HEADERS:

Accept:     application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; en-us) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10

RESPONSE HEADERS:

Age:              1952
Cache-Control:    public, max-age=3598
Connection:       keep-alive
Content-Encoding: gzip
Content-Length:   2060
Content-Type:     text/javascript; charset=utf-8
Date:             Fri, 13 Nov 2009 22:54:18 GMT
Etag:             "272d9ec2e59aa92da18758cf42a4d729"
Server:           nginx/0.7.61 + Phusion Passenger 2.2.5 (mod_rails/mod_rack)
Status:           200 OK
Via:              1.1 varnish
X-Powered-By:     Phusion Passenger (mod_rails/mod_rack) 2.2.5
X-Runtime:        0.11573
X-Varnish:        176673116 176651738

LINK: URL that does not give a warning in IE

REQUEST HEADERS:

Accept:     application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; en-us) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10

RESPONSE HEADERS:

Age:              14
Connection:       keep-alive
Content-Encoding: gzip
Content-Length:   52
Content-Type:     text/javascript
Date:             Fri, 13 Nov 2009 22:55:03 GMT
Etag:             "7b7ded6696ee52551289c856d3173db4"
Last-Modified:    Fri, 13 Nov 2009 22:30:45 GMT
Server:           AmazonS3
Via:              1.1 varnish
X-Amz-Id-2:       CR79uoLC67sr0e0uj4CUOCoBQgcIW/jaJc/FNSA3zsK3Lns/gAqx98/T9h/UeJGm
X-Amz-Request-Id: BCF2F2D69F5126DD
X-Varnish:        1566212056 1566211955

What immediately sticks out to me is the Content-Type of "text/javascript; charset=utf-8" for the URL that gives a warning - is that valid? I had always assumed that only "text/javascript" would be valid.

Also, the URL that gives a warning returns content of Content-Type "text/javascript," but it is a Rails URL that does not have an extension of .js - could that make a difference?

Is there anything else that sticks out, or does anyone have any other ideas of what could be causing this problem? Thanks very much for any help.

最佳回答

It s been some time and I ve since forgotten the details of this problem, but I do know that the ultimate cause was due to a POST request that returned a Content-Type of "text/javascript."

Apparently, IE interprets POSTs that return "text/javascript" in the response headers as a security threat, which causes it to display the dreadful, and terribly uninformative, error message bar.

We changed the Content-Type of the response to "text/html" and that solved it!

问题回答

First thing, I would try changing an extension to .js as you suggested. Internet Explorer has some nasty extension (among other things) based heuristics for determining document type.

It also sniffs the content in some cases, so the beginning should be typical for this type of a file (no weird characters, e.t.c.)

Make sure your JQuery statment looks something like this:

$.ajax({
  type: "GET",
  url: "test.js",
  dataType: "script"  // Defaults to HTML
});




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签