...

View Full Version : Creating a file in Javascript using the available Bytestream



rizmaverick
06-26-2012, 09:33 AM
Is there any way to create a file (eg: a Pdf) in Javascript by setting its mime type,title, content, etc and display it in the browser with the option to download?

I have a Base64 bytestream of a pdf in the javascript and I am using it to display the pdf to the user by:

document.location.href = 'data:application/pdf;base64,' +data;
which opens the file in a new window (it works in all browsers except IE as it has url restrictions) and the file is not downloadable in all browsers. IE is the primary browser for testing my application so this is a big issue.

Option of using a backend technology like Java to create a file and send it as the response is ruled out as I have an ABAP backend and it can give me only the bytestream. Many suggested using jsPdf but jsPdf creates the content from javascript (like setting line by line info and formatting) and uses it. But as I already have all the data and formatting I need I am not able to use it.

Here is the question I asked in stackoverflow for byte conversion and display of pdf in the webpage, that got answered, for additional info:

http://stackoverflow.com/questions/11131789/how-to-convert-hexadecimal-byte-stream-in-the-form-of-string-to-actual-bytestrea

I have done my best in searching for the solution but couldn't able to find one yet. Please help. I will be thankful.
Regards, Riswan

felgall
06-26-2012, 10:13 AM
JavaScript running in the browser has very limited file access - cookies and localStorage only and not separate files. Only when you run JavaScript outside the browser (on the server or on the desktop) is file access permitted.

This block is deliberate for security reasons - otherwise JavaScript could be used to trash your computer.

nicholagi
06-26-2012, 07:12 PM
I have a Base64 bytestream of a pdf in the javascript and I am using it to display the pdf to the user by:

document.location.href = 'data:application/pdf;base64,' +data;

which opens the file in a new window (it works in all browsers except IE as it has url restrictions) and the file is not downloadable in all browsers. IE is the primary browser for testing my application so this is a big issue.

Option of using a backend technology like Java to create a file and send it as the response is ruled out as I have an ABAP backend and it can give me only the bytestream. Many suggested using jsPdf but jsPdf creates the content from javascript (like setting line by line info and formatting) and uses it. But as I already have all the data and formatting I need I am not able to use it.

rnd me
06-26-2012, 08:28 PM
use the save button inside the pdf viewer to save...

dineshkumarwin
11-06-2013, 09:41 AM
I tried this code. It's working for me...

function download(strData, strFileName, strMimeType) {
var D = document, A = arguments, a = D.createElement("a"),
d = A[0], n = A[1], t = A[2] || "application/pdf";

var newdata = "data:" + strMimeType + ";base64," + escape(strData);

//build download link:
a.href = newdata;

if ('download' in a) {
a.setAttribute("download", n);
a.innerHTML = "downloading...";
D.body.appendChild(a);
setTimeout(function () {
var e = D.createEvent("MouseEvents");

e.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null
);
a.dispatchEvent(e);
D.body.removeChild(a);
}, 66);
return true;
};
}

rnd me
11-06-2013, 04:30 PM
I tried this code. It's working for me...

function download(strData, strFileName, strMimeType) {
var D = document, A = arguments, a = D.createElement("a"),
d = A[0], n = A[1], t = A[2] || "application/pdf";

var newdata = "data:" + strMimeType + ";base64," + escape(strData);

//build download link:
a.href = newdata;

if ('download' in a) {
a.setAttribute("download", n);
a.innerHTML = "downloading...";
D.body.appendChild(a);
setTimeout(function () {
var e = D.createEvent("MouseEvents");

e.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null
);
a.dispatchEvent(e);
D.body.removeChild(a);
}, 66);
return true;
};
}

gee, that looks familiar to me, i wonder what genius wrote it?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum