How to force save as a dialog in firefox besides changing the headers?

Is there a way to force save the save dialog for www.example.com/example.pdf to ff? (I can not change the headers)

+6
javascript firefox save
May 7 '09 at 5:55
source share
2 answers

If you can output the file to the client in base64, you can use uris to download to download.

location.href = 'data:application/octet-stream;base64,' + appendPDFContentHere OR <a href="data:application/octet-stream;base64,appendPDFContentHere">pdf</a> 

This will only work in browsers other than IE, but as you requested Firefox, this should work well.

EDIT:

Both of the examples below contain bytes for creating PNGs. If you click the first, you can view the image, as usual, in the browser. however, if you click the second link, it will force you to upload the image. Save it as .png and you will see that it is the same image. The only difference in the two links is the mime type

view image ( preview shortened URL ) - mime type: image / png

download image ( Preview abbreviated URL ) - mime type: application / octet-stream

You asked what you put appendPDFContentHere instead, and the answer is the base64 encoded bytes that make up the PDF. I used this online base64 encoder to encode the image used in the example.

+4
May 7, '09 at 6:12
source share

The only thing I can think of is to change the settings of Firefox. I assume you cannot do this.

EDIT:

I have combined a client solution based on the idea of ​​a data URI. It uses a modified version of the base64 encoder and the binary XMLHTTPrequests script technique downloads the PDF file, then generates and places the data URI link dynamically using the base64 encoder.

This should be useful when you want to encode an octet stream, but don't have access to the server (as this seems to apply to the OP).

Notice, I just posted an example using hunts.pdf with which the OP was tested.

 /** * * Base64 encode / decode * http://www.webtoolkit.info/ * **/ var Base64 = { // private property _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", // public method for encoding encode : function (input) { var output = ""; var chr1, chr2, chr2raw, chr3, chr3raw, enc1, enc2, enc3, enc4; var i = 0; //input = Base64._utf8_encode(input); while (i < input.length) { chr1 = input.charCodeAt(i++) & 0xFF; chr2 = isNaN(chr2raw = input.charCodeAt(i++)) ? NaN : (chr2raw & 0xFF); chr3 = isNaN(chr3raw = input.charCodeAt(i++)) ? NaN : (chr3raw & 0xFF); enc1 = chr1 >> 2; enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); enc4 = chr3 & 63; if (isNaN(chr2)) { enc3 = enc4 = 64; } else if (isNaN(chr3)) { enc4 = 64; } output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4); } return output; } } // http://web.archive.org/web/20071103070418/mgran.blogspot.com/2006/08/downloading-binary-streams-with.html //fetches BINARY FILES synchronously using XMLHttpRequest load_url = function(url) { var req = new XMLHttpRequest(); req.open('GET',url,false); //XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com] req.overrideMimeType('text/plain; charset=x-user-defined'); req.send(null); if (req.status != 200){ alert(req.status); return ''; } return req.responseText; } function getDataURI(filename) { var file = load_url(filename); var uueFile = Base64.encode(file); var uri = 'data:application/octet-stream;base64,' + encodeURIComponent(uueFile); return uri; } window.addEventListener("load", function() { var link = getDataURI("foo.pdf"); document.getElementById("myDiv").innerHTML += '<a href="' + link + '"><code>' + link + '</code></a><br><br>'; } 
+2
May 07 '09 at 5:57 a.m.
source share



All Articles