使用ajax上传文件
我正在寻找一种可用于文件上传的AJAX方法。 如果我找到一个使用 Prototype + Script.acul.us 的函数那就太好了
I am looking for an AJAX method that can be used to file upload.
It will be super if I find a function that uses Prototype + Script.acul.us
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用纯 Ajax 无法上传文件,因为 Javascript 无法直接访问本地文件。
jQuery 表单插件 等工具通过动态生成不可见的 iframe 并提交到该框架来解决此问题。
可能也有一个基于原型或独立于框架的解决方案。
File uploads are not possible using pure Ajax, because Javascript can't get direct access to the local file.
Tools like the jQuery form plugin work around this by generating an invisible iframe on the fly, and submitting to that.
There probably is a Prototype based, or framework independent solution for this as well.
对此没有原生的ajax支持,但是Uploadify使用flash做了一个解决方法(使用jquery,但自2.0以来可以与其他api一起使用)
there is no native ajax support for this, however Uploadify does a workaround using flash (uses jquery but is able to be used with other api's since 2.0)
http://teamco-anthill.blogspot.com /2009/01/ajax-progress-indicator-with-prototype.html
?
http://teamco-anthill.blogspot.com/2009/01/ajax-progress-indicator-with-prototype.html
?
正如其他人指出的那样,JavaScript 无法异步上传文件(否则启用 JavaScript 的网站可能会窃取您硬盘上的任何文件)。最好的方法是:
iframe 方法意味着重新加载的是 iframe,而不是显示给用户的页面,这样对于最终用户来说,体验看起来就像就好像它是阿贾克斯一样。
如果您使用的是 Rails,我猜您可能是因为使用了 Prototype 和 scriptaculous,那么有一个可用的插件可以为您处理这种方法的棘手部分,名为 responds_to_parent,自从我自己在 Rails 应用程序中实现此功能以来已经有很长时间了,但是 此博客中的第 6 步 看起来是如何使用它的一个很好的示例。
As others have pointed out, javascript is not able to asynchronously upload files (otherwise javascript enabled sites could steal any file off your harddrive). The best approaches are:
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.
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.