读取“文件”的内容目的? [英] Read the contents of a "file" object?

查看:127
本文介绍了读取“文件”的内容目的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个文件对象(通过处理从桌面拖放的文件来检索)。我可以使用ajax将文件发送到服务器,然后将其扔回来处理它们。但是,是否可以阅读它的内容而不做这些?

So I have a "File" object (retrieved by handling file drag and drop from desktop). I can send the files to the server with ajax, and then throw them back for javascript to handle them. But is it possible to read the contents of it without doing all this?

在这里,玩弄这个小提琴。将任何文件拖动到框中,并使用变量文件

Here, play around with this fiddle. Drag any file to the box and use the variable file.

我已经尝试了这个对象的所有方法...没有运气。你可以得到你刚刚拖到浏览器中的文件的内容吗?

I've already tried all of the methods of this object...no luck. Can you get the contents of the file you just dragged into the browser?

PS:我会把这些文件发送到服务器:

PS: I would send the files to the server like this:

var ajaxRequest = new XMLHttpRequest();
ajaxRequest.open("returnRawPostData.php");
ajaxRequest.send(file);

我可能错过了上面的代码,但这只是因为没有使用普通的JS再次执行AJAX调用。

I might have missed something in the code above, but that's just because one doesn't use plain JS to do AJAX calls anymore.

推荐答案

使用Martin Mally的链接(非常感谢! ,我想出了这一点:

Using the links from Martin Mally (thanks a lot!), I came up with this:

var file = e.dataTransfer.files[0],read = new FileReader();

read.readAsBinaryString(file);

read.onloadend = function(){
    console.log(read.result);
}

其中 read.result 保存文件的内容。

这篇关于读取“文件”的内容目的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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