0

I got some code off of here and I tried to use it, but when I tried it, it didn't work as I had expected it to. Can you help fix the code so that a dialog comes up to save the file, instead of it downloading automatically. It will probably just need a minor tweak with the functions...

Here is the HTML...

<table>
    <tr><td>Text to Save:</td></tr>
    <tr>
        <td colspan="3">
            <textarea id="inputTextToSave" style="width:512px;height:256px"></textarea>
        </td>
    </tr>
    <tr>
        <td>Filename to Save As:</td>
        <td><input id="inputFileNameToSaveAs"></input></td>
        <td><button onclick="saveTextAsFile()">Save Text to File</button></td>
    </tr>
    <tr>
        <td>Select a File to Load:</td>
        <td><input type="file" id="fileToLoad">
        <td><button onclick="loadFileAsText()">Load Selected File</button><td>
    </tr>
</table>

And the Javascript...

function saveTextAsFile() {
    var textToWrite = document.getElementById("inputTextToSave").value;
    var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
    var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;
    var downloadLink = document.createElement("a");
    downloadLink.download = fileNameToSaveAs;
    downloadLink.innerHTML = "Download File";
    if (window.webkitURL != null) {
        // Chrome allows the link to be clicked
        // without actually adding it to the DOM.
        downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
    } else {
        // Firefox requires the link to be added to the DOM
        // before it can be clicked.
        downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
        downloadLink.onclick = destroyClickedElement;
        downloadLink.style.display = "none";
        document.body.appendChild(downloadLink);
    }
    downloadLink.click();
}

function destroyClickedElement(event) {
    document.body.removeChild(event.target);
}

function loadFileAsText() {
    var fileToLoad = document.getElementById("fileToLoad").files[0];
    var fileReader = new FileReader();
    fileReader.onload = function(fileLoadedEvent) {
        var textFromFileLoaded = fileLoadedEvent.target.result;
        document.getElementById("inputTextToSave").value = textFromFileLoaded;
    };
    fileReader.readAsText(fileToLoad, "UTF-8");
}

EDIT: This question is unique as I am not asking how to force download, I am asking how to open a 'Save As' dialog box, like one that appears when you save an image

EDIT: There may be an answer to this with ActiveX?

EDIT: There seems to be no way of doing this with ActiveX, but the program still has a file name box. Why is this being ignored by the program? The download's file name is a (what looks like) randomly generated number?

  • The original does show the file download dialog as expected. (If it doesn’t for you, then that is likely due to your browser settings.) – CBroe Feb 16 '16 at 14:10
  • Possible duplicate of [Force to open "Save As..." popup open at text link click for pdf in HTML](http://stackoverflow.com/questions/3802510/force-to-open-save-as-popup-open-at-text-link-click-for-pdf-in-html) – Ageonix Feb 16 '16 at 14:12
  • It is very likely your browser settings. You can just go to your settings and configure your browser so that it asks you where to download to. – Glubus Feb 16 '16 at 14:13
  • That's NOT what the OP is aksing. The OP is asking how to code it so that any user will be prompted. – Scott Marcus Feb 16 '16 at 14:15
  • https://jsfiddle.net/5u1kxe22/ this fiddle works great, only `Load selected file` button doesnt work – AshBringer Feb 16 '16 at 14:16
  • FYI - in the fiddle, the input element has a closing tag. input elements don't get closed. – Scott Marcus Feb 16 '16 at 14:20
  • The ONLY case where ActiveX is viable is with a close/controlled user base since ActiveX is only for IE and most users will click "No" when prompted to download and install the control. – Scott Marcus Feb 16 '16 at 14:34

1 Answers1

-1

If you are asking to get the traditional "Save As..." dialog, there is no way through JavaScript. Browsers download files according to their default settings, which may be directly to a default download location or a dialog box may be provided, but again this is based on the client's settings, not JavaScript.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • 2
    This is not true, some browsers do this by default, but there's always a way to configure it. – Glubus Feb 16 '16 at 14:13
  • Which browsers do this by default? What is the way to configure it so that it will work in all browsers. If you can't answer this, please retract your downvote. – Scott Marcus Feb 16 '16 at 14:14
  • I didn't downvote, someone else did. I didn't mean you can configure it on your server somewhere, Im saying the browser as in it being the client you're using to download the file is configurable. Also if you want a list of all browsers that do this by default I'd redirect("www.google.com") you. – Glubus Feb 16 '16 at 14:16
  • I understood what you meant (that a user can configure their browser to save the file where they want). That's not what the OP is asking. The OP wants code that produces a dialog for all users that prompts them at the time of download to choose where the file goes and what it will be called. – Scott Marcus Feb 16 '16 at 14:19
  • Right, and then you said: "Browsers download files to their default download location - No dialog for saving is provided" which is untrue, as it depends on the settings of the user's browser config. – Glubus Feb 16 '16 at 14:24
  • I don't think you understand. Browsers don't provide a Save As... dialog via JavaScript code. We're not talking about what a user can configure in their own personal settings. The OP is asking for a code solution that makes this happen for ALL users EACH time the file is downloaded. Browsers don't do that from JavaScript. – Scott Marcus Feb 16 '16 at 14:27
  • I'm not arguing what you're trying to convey, I'm arguing about what you're providing as an answer. You're saying that browsers never provide a Save As dialog which is simply untrue. You should edit your answer so that it properly sends your message: "There is no way to prompt a Save As dialog through javascript", rather than "Browsers never prompt a Save As dialog". EDIT: I'm not trying to harass you here, like I said, I didn't downvote, Im simply telling you that you need to be precise in your answering. – Glubus Feb 16 '16 at 14:34