这篇文章主要介绍了jquery ajax方式上传文件的方法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
jquery ajax方式上传文件用到两个对象
第一个对象:formdata
第二个对象:xmlhttprequest
目前新版的firefox 与 chrome 等支持html5的浏览器完美的支持这两个对象,但ie9尚未支持 formdata 对象,还在用ie6 ? 只能仰天长叹....
有了这两个对象,我们可以真正的实现ajax方式上传文件。
示例代码:
<!doctype html>
<html>
<head>
<title>html5 ajax 上传文件</title>
<script type="text/javascript">
function upladfile() {
var fileobj = document.getelementbyidx_x_x("file").files[0]; // 获取文件对象
var filecontroller = "../file/save"; // 接收上传文件的后台地址
// formdata 对象
var form = new formdata();
form.append("author", "hooyes"); // 可以增加表单数据
form.append("file", fileobj); // 文件对象
// xmlhttprequest 对象
var xhr = new xmlhttprequest();
xhr.open("post", filecontroller, true);
xhr.onload = function () {
alert("上传完成!");
};
xhr.send(form);
}
</script>
</head>
<body>
<input type="file" id="file" name="myfile" />
<input type="button" onclick="upladfile()" value="上传" />
</body>
</html>
很简洁的代码,便可以达到ajax方式上传文件,上面的代码中使用<input type="file" />这种传统的选择文件的方法产生文件对象,html5还支持使用多种更灵活的方式,如拖拽文件到指定的元素上产生。
ajax已成功上传文件,但这时我们会想到一个问题,如何显示进度条?带着这个问题,脑子会想到,flash? 浏览器插件?。
no,现在不需要这些东西了。
开始着手,先做一个进度条,进度条也很简单,使用html5 新加的标签:
<progress id="progressbar" value="0" max="100"> </progress>
这个在浏览器中便会呈现了一个进度条,现在我们要做的就是在上传的时候,实时的去改变它的value值,然后进度显示的问题便交给它了。
我们的服务器端无需修改,只需要在js中xhr对象加一个事件。
xhr.upload.addeventlistener("progress", progressfunction, false)
progressfunction 被调用的时候会传进一个事件对象,这个对象有两个属性,一个就是loaded 一个是total ,分别代表,已上传的值,和总要上传的值。
这正是我们需要的,所以这个方法,可以这样写:
function progressfunction(evt) {
var progressbar = document.getelementbyidx_x_x("progressbar");
if (evt.lengthcomputable) {
progressbar.max = evt.total;
progressbar.value = evt.loaded;
}
}
这样便可以完成,上传进度显示了。
如下针对上面的第一个示例代码,做一个调整:
示例代码2,带进度显示:
<!doctype html>
<html>
<head>
<title>html5 ajax 上传文件</title>
<script type="text/javascript">
function upladfile() {
var fileobj = document.getelementbyidx_x_x("file").files[0]; // js 获取文件对象
var filecontroller = "../file/save"; // 接收上传文件的后台地址
// formdata 对象
var form = new formdata();
form.append("author", "hooyes"); // 可以增加表单数据
form.append("file", fileobj); // 文件对象
// xmlhttprequest 对象
var xhr = new xmlhttprequest();
xhr.open("post", filecontroller, true);
xhr.onload = function () {
// alert("上传完成!");
};
xhr.upload.addeventlistener("progress", progressfunction, false);
xhr.send(form);
}
function progressfunction(evt) {
var progressbar = document.getelementbyidx_x_x("progressbar");
var percentagep = document.getelementbyidx_x_x("percentage");
if (evt.lengthcomputable) {
progressbar.max = evt.total;
progressbar.value = evt.loaded;
percentagep.innerhtml = math.round(evt.loaded / evt.total * 100) + "%";
}
}
</script>
</head>
<body>
<progress id="progressbar" value="0" max="100">
</progress>
<span id="percentage"></span>
<br />
<input type="file" id="file" name="myfile" />
<input type="button" onclick="upladfile()" value="上传" />
</body>
</html>
后台接收文件的程序可以是任何语言(c#,php,python 等)编写的,上述例子使用c#
很简单,无需为这个进度条做任何改动。
var flist = request.files;
for (int i = 0; i < flist.count; i++)
{
string filepath = "e:\\hooyes\\files\\";
var c = flist[i];
filepath = path.combine(filepath, c.filename);
c.saveas(filepath);
}
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
利用ajax实现异步刷新请求
关于ajax技术中servlet末尾的输出流
ajax+php实现商品分类三级联动
以上就是jquery ajax方式上传文件的方法的详细内容。