简体   繁体   中英

JavaScript - Can I show the content of a file in an iFrame?

In my server (TFS) I can access a file and get his content by a GET request, The problem is that the Content-Type of the response is application/octet-stream;

And when I'm trying to load this file in an iFrame I get the download windows popup to save this file ,

Is there a way to put the file content in this case in the iFrame? or some other way to get this file content in Javascript (XMLHttpRequest having some problems with application/octet-stream;)?

It's not able to change the content-type of the response of get request in TFS. By default is application/octet-stream.

This depends on the type of the file you access. When you get "application/octet-stream" response, you may access some binary files or the file extension cannot be recognized. In this condition, you will get the response below which will suggest the agent to download the file:

content-type: application/octet-stream;
content-disposition: attachment

See HTTP Standard for details:

If this header is used in a response with the application/octet- stream content-type, the implied suggestion is that the user agent should not display the response, but directly enter a `save response as...' dialog.

If you are accessing some general file types, you will get different response. For example:

a.txt -> content-type →text/plain;
a.ps -> content-type →application/json;
1.png -> content-type →image/png;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM