我正在做一个加密数据并将其发送到服务器的项目,所以我需要将数据存储在一个文件中,然后将其发送到服务器(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/