javascript - 使用javascript创建新的文本文件()但存储[objectFile]而不是文本

标签 javascript

我正在做一个加密数据并将其发送到服务器的项目,所以我需要将数据存储在一个文件中,然后将其发送到服务器(php)。然后用户在客户端抓取文件进行解密。但是我遇到了将数据存储在文本文件中或打开它的问题。

我创建文件没有问题,但是当我打开文件/使用 FileReader() 时,它说:[objectFile] 而不是数据。

我做错了什么?下面是一个例子,我尽量让它尽可能简单。 注意:我看不懂 jquery。

<!DOCTYPE html>
<html>
    <head>
        <script>
            function createdTextFile(){
                var date = new Date();
                var file = new File(["text text text text text text"], "textfile.txt", {type: "text", lastModified: date});
            /*
                sent file to server
                var formdata = new FormData();
                formdata.append("Uploaded_file", file);                     
            */

                //Created download link
                var uriContent = "data:application/octet-stream,"+file;
                var div_idFileDownload = document.getElementById("file_dowload");
                div_idFileDownload.innerHTML = "";
                var createElement_aFileDownload = document.createElement("a");
                    createElement_aFileDownload.setAttribute("download", "textfile.txt");
                    createElement_aFileDownload.setAttribute("href", uriContent);
                    div_idFileDownload.appendChild(createElement_aFileDownload);
                var nm = document.createTextNode("textfile.txt");
                    createElement_aFileDownload.appendChild(nm);
            }
            //open text file: [objectFile]
            //Need the text file to say: text text text text text text
        </script>
    </head>
    <input type="file" id="upload_img">
    <input type="button" onclick ="createdTextFile()" value="download">
    <div id="file_dowload"><div/>
</html>

最佳答案

试试吧,你可以测试看到这个工作here :

function createdTextFile(){
    var date = new Date();
    var file = new File(["text text text text text text"], "textfile.txt", {type: "text", lastModified: date});
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function () {

        //Created download link
        //var uriContent = "data:application/octet-stream,"+file;
        var uriContent = reader.result;
        var div_idFileDownload = document.getElementById("file_dowload");
        div_idFileDownload.innerHTML = "";
        var createElement_aFileDownload = document.createElement("a");
        createElement_aFileDownload.setAttribute("download", "textfile.txt");
        createElement_aFileDownload.setAttribute("href", uriContent);
        div_idFileDownload.appendChild(createElement_aFileDownload);
        var nm = document.createTextNode("textfile.txt");
        createElement_aFileDownload.appendChild(nm);
    };


}

关于javascript - 使用javascript创建新的文本文件()但存储[objectFile]而不是文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36950067/

相关文章:

javascript - 创建后更新 Apollo 客户端身份验证链接

javascript - 这个表达式是否总是从左到右计算?

javascript - 如何在android中从服务器发送命令功能而不发送请求?

javascript - 在外部浏览器中打开链接

javascript - 数组推送仍然使用以前的引用吗?

javascript - 如何将 input-group-addon ( Bootstrap CSS ) 添加到 Alpaca.js 字段

javascript - 散列映射要按时间范围作为键的对象的最有效方法?

javascript - 数组中的流行词

javascript - 比较两个日期我的javascript

javascript - 覆盖backbone的 View 函数