为什么我不能上传文件异步? [英] Why can't I upload files asynchronously?

查看:200
本文介绍了为什么我不能上传文件异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全日制的学习和功放后;&安培;努力,我终于放弃了上传文件通过纯AJAX (PS:这篇文章<一个href="http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery">How我可以上传文件异步JQuery的?的埋葬我最后的希望)

After a whole-day's study && trying, i finally give up upload files by pure AJAX (ps: this post How can I upload files asynchronously with JQuery? buried my last hope)

我的问题可能有点意义,但我还是想知道为什么AJAX(或XMLHtt prequest)无法处理呢?为什么不能将文件转移像真正的HTT prequest?

My question may be a little meaningless, but i still wanna know why ajax(or XMLHttpRequest) can't handle this? why can't the files transfered like the real httprequest?

推荐答案

JavaScript不能读取本地文件,出于安全考虑,所以使用AJAX,我们不能发送数据。

Javascript cannot read local files for security reasons, so we can't send the data using AJAX.

不过,你可以发布一个标准的HTML表单,并设置表单对象是页面上的一个无形的iframe中。那么这个iframe中可以使用服务器端code来处理上传。

However you can POST a standard HTML form, and set the form target to be an invisible iframe on the page. This iframe can then use server side code to handle the upload.

如果你的理由要使用AJAX是因为你想一个进度条的文件上传,使用这种方法,您可以使用服务器端的静态变量来存储文件上传的进度,然后使用AJAX来使一个简单的,定时的请求,刚刚返回上传进度另一个HTTP页面。

If your reason to use AJAX is because you'd like a progress bar as the file is uploading, using this method you can use a server-side static variable to store the progress of the file upload, then use AJAX to make a simple, timed request to another HTTP page which just returns the upload progress.

这篇关于为什么我不能上传文件异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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