【问题标题】:Keep javascript blob from editing data when saving file保存文件时阻止 javascript blob 编辑数据
【发布时间】:2016-11-08 01:00:43
【问题描述】:

我想将 javascript 中生成的数据移动到文件中

我正在使用

    function saveTextAsFile(textToWrite,FileName){
    var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
    var downloadLink = document.createElement("a");
    downloadLink.download = FileName;
    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.style.display = "none";
        document.body.appendChild(downloadLink);        
    }
    downloadLink.click();
    }

基于来自的代码 Is it possible to write data to file using only JavaScript?

问题是在每个 ascii 值大于 0x79 的字符之前插入一个 0xc2。

000041e0  30 35 5d 22 57 69 72 65  6c 65 73 73 22 3d 30 0a  |05]"Wireless"=0.|
000041f0  00 00 c2 b0 c2 a0 c2 80  7f                       |.........|
000041f9

这发生在 Ubuntu Linux 的 firefox 和 chromium 浏览器中。 我希望除了“text/plain”之外的其他一些 blob 类型不会有这种行为,但我在查找相关文档时遇到了麻烦。

达斯汀·苏达克

注意:这是一种新的提问方式 Can you make a textarea which doesn't automatically edit your text? 这似乎是不可能的

【问题讨论】:

  • above 0x79 - 你的意思是 0x7f - 因为我看到一个 0x7f 没有前面的 0xc2
  • 试试 type: 'text/plain; charset=iso-8859-1' - 因为 0xc2 前缀是 UTF-8 字符从 0x80 到 0xBF 的“编码”方式 - 0xC0 到 0xFF 将获得 0xc3 前缀
  • 或试试type: 'application/octet-binary'
  • 是的,我的意思是 0x7f。我尝试了这两个,但它仍然发生。还有,c0->c380、c1->c381 等。
  • 也许它与使用字符串初始化 blob 有关(即使我在字符串的最后部分进行了十六进制打印以确保它符合预期)

标签: javascript html blob


【解决方案1】:

我在我的谷歌搜索中添加了“应用程序/八位字节二进制”并在以下位置找到了答案 “Create binary blob in JS”。 看起来如果您从 Uint8Array 而不是字符串初始化 blob,它不再改变数据。这是完整的工作代码:

    function saveTextAsFile(textToWrite,FileName){
       function destroyClickedElement(event){
            document.body.removeChild(event.target);
        }
        var byteArray = new Uint8Array(textToWrite.length);
        for (var i=0;i<byteArray.length;i++){
            byteArray[i]=textToWrite.charCodeAt(i);
        }
        var textFileAsBlob = new Blob([byteArray], {type:'application/octet-binary'});
        var downloadLink = document.createElement("a");
        downloadLink.download = FileName;
        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();
    }

【讨论】:

    猜你喜欢
    • 2016-11-09
    • 2013-09-23
    • 1970-01-01
    • 2020-05-02
    • 2022-06-13
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多