簡體   English   中英

如何從textarea保存文件並在下載代碼中配置路徑? JavaScript

[英]How to save file from textarea and configure path in download code? JavaScript

單擊下載文件后的結果是我計算機上的文件夾下載,但是當我單擊保存文件時,我想要一個像“\\directory\\subdirectory”這樣的特定路徑。
如果 javascript 不可能,我該怎么做?

<input id="inputFileNameToSaveAs" style="width: 400px;"></input>
<button onclick="saveTextAsFile()" style="width: 250px;">Save File</button>
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);
            }

沒有這樣做的可能性。 您可以下載文件,但不能將其寫入特定文件夾。 出於安全考慮,瀏覽器在沙箱中運行,無法訪問您的整個計算機。

您不希望在您輸入的隨機頁面上使用一些 javascript 來弄亂您的計算機文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM