保存文件时防止javascript blob编辑数据 [英] Keep javascript blob from editing data when saving file

查看:82
本文介绍了保存文件时防止javascript blob编辑数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to move data generated in javascript to a file

我正在使用

    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();
    }

基于以下代码 是否可以写入数据只能使用JavaScript归档?

问题在于,每个ascii值大于0x79的字符之前都插入了0xc2.

The problem is that a 0xc2 is inserted before every character with ascii value above 0x79.

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

这既发生在Firefox& Ubuntu Linux中的Chrome浏览器. 我希望除文本/纯文本"之外的其他其他Blob类型不会有这种行为,但是我在查找相关文档时遇到了麻烦.

This happened in both firefox & chromium browsers in Ubuntu Linux. I'm hoping that some other blob type besides 'text/plain' will not have this behavior, but I'm having trouble finding the relevant documentation.

Dustin Soodak

Dustin Soodak

注意:这是一种新的提问方式 您可以制作一个不会自动生成的文本区域吗?编辑您的文字? 这似乎是不可能的

Note: this is a new approach to question Can you make a textarea which doesn't automatically edit your text? which seems to be impossible

推荐答案

我在Google搜索中添加了"application/octet-binary",并在以下位置找到了答案 "在JS中创建二进制blob ". 看起来,如果您从Uint8Array而不是字符串初始化Blob,它将不再更改数据.这是完整的工作代码:

I added 'application/octet-binary' to my google search and found an answer at "Create binary blob in JS". It looks like if you initialize the blob from a Uint8Array instead of a string, it no longer alters the data. Here is the full working code:

    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();
    }

这篇关于保存文件时防止javascript blob编辑数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆