javascriptdownloadblobencodeuricomponentcreateobjecturl

Creating and downloading text file from string in JavaScript: Blob/createObjectURL vs. encodeURIComponent


As I've been looking for a way to create and download a text file from a website with JavaScript, I've found a bunch of solutions but generally using either Blob/createObjectURL or otherwise encodeURIComponent, with the former being more popular from what I've seen. Below I show two examples: note that only one-two lines in the beginning are different in the two functions (which I noted in comments).

Blob/createObjectURL:

function dl_as_file_Blob(filename_to_dl, data_to_dl) {
    let blobx = new Blob([data_to_dl], { type: 'text/plain' }); // ! Blob
    let elemx = window.document.createElement('a');
    elemx.href = window.URL.createObjectURL(blobx); // ! createObjectURL
    elemx.download = filename_to_dl;
    elemx.style.display = 'none';
    document.body.appendChild(elemx);
    elemx.click();
    document.body.removeChild(elemx);
}

encodeURIComponent:

function dl_as_file_URI(filename_to_dl, data_to_dl) {
    let elemx = document.createElement('a');
    elemx.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(data_to_dl); // ! encodeURIComponent
    elemx.download = filename_to_dl;
    elemx.style.display = 'none';
    document.body.appendChild(elemx);
    elemx.click();
    document.body.removeChild(elemx);
}

What I'd like to know is whether there is any reason to prefer one over the other. So far I could find two small differences. First, encodeURIComponent is more widely supported by browsers than createObjectURL. Second, Blob seems not to support encoding. Based on this, I'd choose the encodeURIComponent solution, but I wonder if there is a reason for why I see the Blob/createObjectURL solution more often.

EDIT: The question above is quite general, so let me narrow down a bit for my specific use case: I want to allow users to download a simple (utf-8), relatively small (max 100-200 kB) text (the results of a completed self-assessment test). There is no really sensitive data involved, and the file is only needed for this purpose alone, on the client-side. Nonetheless, I also welcome more general answers, since I'm curious about the differences.


Solution

  • I wonder if there is a reason for why I see the Blob/createObjectURL solution more often.

    IMHO there are a few possible reasons:

    TL;DR

    1. Performance

    2. Security

    3. API access

    4. It is an object

    5. It looks cooler


    1. Performance

    Most importantly, client-side JavaScript File object is a subtype of Blob, a File is just a Blob of data: with a name and a modification date. You can obtain File objects from <input type="file"> elements and from the drag-and-drop API.

    2. Security

    3. API access

    Once you have a Blob, there are various things you can do with it, many of them symmetrical to the items above:

    4. It is an object

    5. It looks cooler

    sources: