Set / Change MIME type of iFrame?

别说谁变了你拦得住时间么 提交于 2019-12-10 21:05:47

问题


Is is possible to set the MIME type for an iFrame dyanamically with JavaScript? Same question for DIV?


回答1:


No.

The MIME type is determined by what you load into it.

For a frame that will be whatever the src specifies.

Divs don't load external content so don't have MIME types.




回答2:


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 8888or 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!



来源:https://stackoverflow.com/questions/12144554/set-change-mime-type-of-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!