使用JavaScript动态设置iFrame的MIME类型是否可能?DIV也一样?
使用JavaScript动态设置iFrame的MIME类型是否可能?DIV也一样?
编号。
MIME类型取决于您加载的内容。
对于一个框架,它将是src
指定的任何内容。
Div不加载外部内容,因此没有MIME类型。
I may have the answer you need. Although our purposes may be different.
I spent days trying to specify MIME type for iframe because I use iframe to include text data into my .htm program. If the text data extension file name is not .txt then my Web browser will save it to local disk unexpectedly instead of appearing in myIframeElement.contentDocument.lastChild.innerText. <=== problem!!
The knowledge I have finally known to resolve the problem is that: it's the Web server not the client browser to specify the iframe MIME type! I used to use python local web server oneliner
python -m SimpleHTTPServer 8888
or python -m http.server 8888
they both response a certain MIME type incorrectly for my data file's proprietary extension name.
My solution is simply to use another local web server. This very short solution on Github works fine for me, Local Web server by Node.js. You can look into the source code to see why it works,
response.writeHead(200);
response.write(file, "binary");
response.end();
... and modify to the MIME type you want for your cases around here.
Example to run this Node.js local web server (Windows): d:\>node.exe static_server.js
I hope this helps!