HTTP Headers for File Downloads
Acoording to RFC 2046 (Multipurpose Internet Mail Extensions):
The recommended action for an
implementation that receives an
"application/octet-stream" entity is
to simply offer to put the data in
a file
So I'd go for that one.
How to set a header for a HTTP GET request, and trigger file download?
Try
html
<!-- placeholder ,
`click` download , `.remove()` options ,
at js callback , following js
-->
<a>download</a>
js
$(document).ready(function () {
$.ajax({
// `url`
url: '/echo/json/',
type: "POST",
dataType: 'json',
// `file`, data-uri, base64
data: {
json: JSON.stringify({
"file": "data:text/plain;base64,YWJj"
})
},
// `custom header`
headers: {
"x-custom-header": 123
},
beforeSend: function (jqxhr) {
console.log(this.headers);
alert("custom headers" + JSON.stringify(this.headers));
},
success: function (data) {
// `file download`
$("a")
.attr({
"href": data.file,
"download": "file.txt"
})
.html($("a").attr("download"))
.get(0).click();
console.log(JSON.parse(JSON.stringify(data)));
},
error: function (jqxhr, textStatus, errorThrown) {
console.log(textStatus, errorThrown)
}
});
});
jsfiddle http://jsfiddle.net/guest271314/SJYy3/
Content-Disposition headers to allow both download and open file
Sadly it doesn't seem like there is any ways to do that with headers only, I will need to get some amount of scripting involved.
HTTP Headers on Protected Downloads
This error is caused by the filesize
call overflowing the systems PHP_MAX_INT which yields a length less then that of the entire file. (Which is about 2GB on a 32bit machine)
Only browsers that trust the content-length header over the size of the file itself appear to be afflicted.
Solution is to change
header("Content-Length: " . filesize($file));
to
header("Content-Length: " . exec("stat -c %s ".escapeshellarg($filename)));
http download file name
You need to append the HTTP response header "Content-Disposition"
Response.AppendHeader("content-disposition", "attachment; filename=\"" + fileName +"\"");
Force file download with php using header()
The problem was that I used ajax to post the message to the server, when I used a direct link to download the file everything worked fine.
I used this other Stackoverflow Q&A material instead, it worked great for me:
- Ajax File Download using Jquery, PHP
Do I need Content-Type: application/octet-stream for file download?
No.
The content-type should be whatever it is known to be, if you know it. application/octet-stream
is defined as "arbitrary binary data" in RFC 2046, and there's a definite overlap here of it being appropriate for entities whose sole intended purpose is to be saved to disk, and from that point on be outside of anything "webby". Or to look at it from another direction; the only thing one can safely do with application/octet-stream is to save it to file and hope someone else knows what it's for.
You can combine the use of Content-Disposition
with other content-types, such as image/png
or even text/html
to indicate you want saving rather than display. It used to be the case that some browsers would ignore it in the case of text/html
but I think this was some long time ago at this point (and I'm going to bed soon so I'm not going to start testing a whole bunch of browsers right now; maybe later).
RFC 2616 also mentions the possibility of extension tokens, and these days most browsers recognise inline
to mean you do want the entity displayed if possible (that is, if it's a type the browser knows how to display, otherwise it's got no choice in the matter). This is of course the default behaviour anyway, but it means that you can include the filename
part of the header, which browsers will use (perhaps with some adjustment so file-extensions match local system norms for the content-type in question, perhaps not) as the suggestion if the user tries to save.
Hence:
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="picture.png"
Means "I don't know what the hell this is. Please save it as a file, preferably named picture.png".
Content-Type: image/png
Content-Disposition: attachment; filename="picture.png"
Means "This is a PNG image. Please save it as a file, preferably named picture.png".
Content-Type: image/png
Content-Disposition: inline; filename="picture.png"
Means "This is a PNG image. Please display it unless you don't know how to display PNG images. Otherwise, or if the user chooses to save it, we recommend the name picture.png for the file you save it as".
Of those browsers that recognise inline
some would always use it, while others would use it if the user had selected "save link as" but not if they'd selected "save" while viewing (or at least IE used to be like that, it may have changed some years ago).
Related Topics
Blocking Comment Spam Without Using Captcha
$_Server['Http_Referer'] Missing
PHP Json_Decode() Returns Null With Valid Json
Convert/Cast an Stdclass Object to Another Class
Fatal Error: Class 'PHPmailer' Not Found
How Does the Keyword "Use" Work in PHP and How to Import Classes With It
How to Get the HTML Code of a Web Page in PHP
How to Migrate My Site from MySQL to MySQLi
Fatal Error: Class 'Soapclient' Not Found
Send Value of Submit Button When Form Gets Posted
Does File_Get_Contents() Have a Timeout Setting
How to Prevent My Site Page to Be Loaded Via 3Rd Party Site Frame of Iframe
Why Is Json_Encode Adding Backslashes
How to Find Out Where a Function Is Defined