Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Creating a file in Javascript using the available Bytestream

    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/1...tual-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

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 584 Times in 565 Posts
    use the save button inside the pdf viewer to save...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;
    };
    }

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 584 Times in 565 Posts
    Quote Originally Posted by dineshkumarwin View Post
    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?
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •