相关疑难解决方法(0)

将文本文件写为blob时无法保留换行符

我有一个文本区域,其中包含我想输出到文本文件供用户下载的文本.

当用户单击"保存"按钮时,我正在使用此功能来抓取它

function saveTextAsFile()
{
    var textToWrite = document.getElementById("inputText").value;
    var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
    alert(textFileAsBlob);
    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 = …
Run Code Online (Sandbox Code Playgroud)

html javascript blob web

14
推荐指数
1
解决办法
1万
查看次数

标签 统计

blob ×1

html ×1

javascript ×1

web ×1