所以我有一个"file"对象(通过处理文件从桌面拖放来检索)。我可以使用 ajax 将文件发送到服务器,然后将它们丢回 javascript 来处理它们。但是是否可以在不执行所有这些操作的情况下阅读其中的内容?
Here, play around with this fiddle.将任何文件拖到框中并使用变量 file
。
我已经尝试了这个对象的所有方法...没有运气。能否获取到刚才拖入浏览器的文件内容?
PS:我会像这样将文件发送到服务器:
var ajaxRequest = new XMLHttpRequest();
ajaxRequest.open("returnRawPostData.php");
ajaxRequest.send(file);
我可能在上面的代码中遗漏了一些东西,但这只是因为人们不再使用纯 JS 来进行 AJAX 调用。
最佳答案
使用来自 Martin Mally 的链接(非常感谢!),我想到了这个:
var file = e.dataTransfer.files[0],
read = new FileReader();
read.readAsBinaryString(file);
read.onloadend = function(){
console.log(read.result);
}
read.result
保存文件的内容。
关于javascript - 读取 "file"对象的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5201317/