简体   繁体   English

使用ajax上传文件

[英]file upload using ajax

I am looking for an AJAX method that can be used to file upload. 我正在寻找一种可用于文件上传的AJAX方法。 It will be super if I find a function that uses Prototype + Script.acul.us 如果我找到一个使用Prototype + Script.acul.us的函数,那将是超级好

File uploads are not possible using pure Ajax, because Javascript can't get direct access to the local file. 使用纯Ajax无法上传文件,因为Javascript无法直接访问本地文件。

Tools like the jQuery form plugin work around this by generating an invisible iframe on the fly, and submitting to that. jQuery表单插件之类的工具可通过动态生成不可见的iframe并提交来解决此问题。

There probably is a Prototype based, or framework independent solution for this as well. 为此,可能还有一个基于原型或与框架无关的解决方案。

没有对此的本机ajax支持,但是Uploadify使用Flash进行了变通(使用jquery,但自2.0起可以与其他api一起使用)

As others have pointed out, javascript is not able to asynchronously upload files (otherwise javascript enabled sites could steal any file off your harddrive). 正如其他人指出的那样,javascript无法异步上传文件(否则,启用了javascript的网站可能会从硬盘驱动器上窃取任何文件)。 The best approaches are: 最好的方法是:

  • A flash uploader, as already suggested 如上所建议的Flash上​​传器
  • Or simulate an Ajax request using an iframe 或使用iframe模拟Ajax请求

The iframe approach means that it's the iframe which reloads, instead of the page displayed to your users, so that to the end user, the experience looks as if it's Ajax. iframe方法意味着重新加载的是iframe,而不是向用户显示的页面,因此对于最终用户而言,体验就好像是Ajax。

If you're using rails, which I gather you might be due to using prototype and scriptaculous, there's a plugin available which handles the tricky bits of this approach for you called responds_to_parent , It's been ages since I implemented this in a rails app myself, but step 6 from this blog looks like a good example of how to use it. 如果您使用的是Rails(可能是由于使用了原型和脚本语言),那么有一个可用的插件可以为您处理这种方法的棘手问题,它称为Reacts_to_parent ,自从我自己在Rails应用程序中实现此方法以来,已有很长时间了,但是此博客中的第6步似乎是如何使用它的一个很好的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM