当前位置:首页 » 文件管理 » ajax文件上传进度

ajax文件上传进度

发布时间: 2023-06-26 16:24:38

① ajax多文件上传如何实现进度条

可以找一个上传插件,如:webupload。
上传插件可以配置显示上传进度,多文件上传可以同时显示多个文件的进度条。
如果自己写的话,需要考虑浏览器兼容和文件上传控制等。

② 哪个javascript框架支持ajax方式的文件上传

7款基于JavaScript和AJAX的文件上传插件,这些插件基本上都能实现以下功能:
多文件上传,拖拽操作,实时上传进度,自定义上传限制
1. jQuery File Upload
具有多文件上传、拖拽、进度条和图像预览功能的文件上传插件,支持跨域、分块、暂停恢复和客户端图像缩放。可与任何服务端平台(如PHP、Python、Ruby on Rails、Java、Node.js、Go等)一起使用,支持标准的HTML表单文件上传。
2. Pixelcone Fileuploader

使用HTML5 API的jQuery文件上传插件,支持AJAX上传和拖拽操作,以及针对老版本浏览器的iframe上传部件。有多种形式来实现多文件上传,每种形式由单一的上传脚本来控制。
3. Ajax Upload
该插件使用XHR来上传多个文件,支持拖拽操作,可以在FF3.6+、Safari4+、Chrome等浏览器中完美运行。
4. Plupload
这是一个针对CMS或类似系统的、高度可用的上传插件。支持分块、拖拽、图像缩放、限制文件大小、显示上传进度等。
5. Uploadify

Uploadify是一个jQuery插件,帮助你在网站中轻松添加多文件上传功能,提供了两个版本(HTML5、Flash)。支持多文件上传、拖拽、实时进度显示,提供了大量的定制功能。
6. Ajax File Upload
该插件是Ajaxupload插件的修改版本,不具备HTML5功能。
7. jQuery FileDrop

该插件使用HTML5 API,允许用户从桌面拖动多个文件到浏览器中,并上传每个文件到用户指定的URL。该插件使用HTML5 FileReader()来读取文件数据。

③ 前端上传文件实时显示进度条和上传速度的工作原理是怎样的

后端的责任。

④ Ajax文件上传进度条如何实现(jquery版本

前端要做的就是设置一个定时器通过接口去后台获取当前上传进度是多少,然后渲染出进度条就行。当进度达到100%时清除定时器。

⑤ jquery ajax xhr监听上传进度显示不准确,求解

如果你是用这种方式上传的话,确实没有好方法。
因为 XMLHttpRequest.onProgress 事件能拿到的是网络传输的字节而已;你说的问题里,“上传进度已完成”,实际是指浏览器已经把文件传输给了服务端;“很久才可以”,是你服务端额外处理的时间,这段时间对浏览器来说是不可感知的,它怎么会知道你服务端处理需要多久呢?
一般处理思路有这么几种:
1、上传进度设置一个最大值,比如 99%,只有当服务端真正返回结果时才会变到 100%,这种方法最为简单粗暴;
2、尽量减少服务端处理的时间,例如收到文件后交给异步队列去处理,立刻返回给客户端响应,这种方法需要额外做的事件比较多,开发难度更高一些;
3、客户端分片上传,把大文件变成若干段小“文件”,缺点是浏览器只有支持 HTML5 才支持 FormData 分片。

⑥ js ajax怎么获取上传的进度

ajax文件上传的进度条实现

<!DOCTYPE html>
<html>
<head>
<title>html5_2.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
#parent{width:550px; height:10px; border:2px solid #09F;}
#son {width:0; height:100%; background-color:#09F; text-align:center; line-height:10px; font-size:20px; font-weight:bold;}
</style>
<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
<script type="text/javascript">
function showPic(){
var pic = $("#pic").get(0).files[0];
$("img").prop("src" , window.URL.createObjectURL(pic) );
uploadFile();
}
function uploadFile(){
var pic = $("#pic").get(0).files[0];
var formData = new FormData();
formData.append("file" , pic);
/**
* 必须false才会避开jQuery对 formdata 的默认处理
* XMLHttpRequest会对 formdata 进行正确的处理
*/
$.ajax({
type: "POST",
url: "upload",
data: formData ,
processData : false,
//必须false才会自动加上正确的Content-Type
contentType : false ,
xhr: function(){
var xhr = $.ajaxSettings.xhr();
if(onprogress && xhr.upload) {
xhr.upload.addEventListener("progress" , onprogress, false);
return xhr;
}
}
});
}
/**
* 侦查附件上传情况 ,这个方法大概0.05-0.1秒执行一次
*/
function onprogress(evt){
var loaded = evt.loaded; //已经上传大小情况
var tot = evt.total; //附件总大小
var per = Math.floor(100*loaded/tot); //已经上传的百分比
$("#son").html( per +"%" );
$("#son").css("width" , per +"%");
}
</script>
</head>
<body>
<img width="400" height="250"/><br />
<input type="file" id="pic" name="pic" onchange="showPic()"/>
<input type="button" value="上传图片" onclick="uploadFile()" /><br />
<div id="parent">
<div id="son"></div>
</div>
</body>
</html>

⑦ 使用jquery.form.js实现文件上传及进度条前端代码

ajax的表单提交只能提交data数据到后台,没法实现file文件的上传还有展示进度功能,这里用到form.js的插件来实现,搭配css样式简单易上手,而且高大上,推荐使用。

需要解释下我的结构, #upload-input-file 的input标签是真实的文件上传按钮,包裹form标签后可以实现上传功能, #upload-input-btn 的button标签是展示给用户的按钮,因为需要样式的美化。上传完成生成的文件名将会显示在 .upload-file-result 里面, .progress 是进度条的位置,先让他隐藏加上 hidden 的class, .progress-bar 是进度条的主体, .progress-bar-status 是进度条的文本提醒。

去掉hidden的class,看到的效果是这样的
[图片上传失败...(image-2c700a-1548557865446)]

将上传事件绑定在file的input里面,绑定方式就随意了。
var progress = $(".progress-bar"), status = $(".progress-bar-status"), percentVal = '0%'; //上传步骤 $("#myupload").ajaxSubmit({ url: uploadUrl, type: "POST", dataType: 'json', beforeSend: function () { $(".progress").removeClass("hidden"); progress.width(percentVal); status.html(percentVal); }, uploadProgress: function (event, position, total, percentComplete) { percentVal = percentComplete + '%'; progress.width(percentVal); status.html(percentVal); console.log(percentVal, position, total); }, success: function (result) { percentVal = '100%'; progress.width(percentVal); status.html(percentVal); //获取上传文件信息 uploadFileResult.push(result); // console.log(uploadFileResult); $(".upload-file-result").html(result.name); $("#upload-input-file").val(''); }, error: function (XMLHttpRequest, textStatus, errorThrown) { console.log(errorThrown); $(".upload-file-result").empty(); } });

[图片上传失败...(image-3d6ae0-1548557865446)]

[图片上传失败...(image-9f0adf-1548557865446)]

更多用法可以 参考官网

⑧ ajax如何 实现 文件上传



程序说明

使用说明

实例化时,第一个必要参数是file控件对象:

newQuickUpload(file);


第二个可选参数用来设置系统的默认属性,包括
属性: 默认值//说明
parameter:{},//参数对象
action:"",//设置action
timeout:0,//设置超时(秒为单位)
onReady:function(){},//上传准备时执行
onFinish:function(){},//上传完成时执行
onStop:function(){},//上传停止时执行
onTimeout:function(){}//上传超时时执行

还提供了以下方法:
upload:执行上传操作;
stop:停止上传操作;
dispose:销毁程序。

varQuickUpload=function(file,options){

this.file=$$(file);

this._sending=false;//是否正在上传
this._timer=null;//定时器
this._iframe=null;//iframe对象
this._form=null;//form对象
this._inputs={};//input对象
this._fFINISH=null;//完成执行函数

$$.extend(this,this._setOptions(options));
};
QuickUpload._counter=1;
QuickUpload.prototype={
//设置默认属性
_setOptions:function(options){
this.options={//默认值
action:"",//设置action
timeout:0,//设置超时(秒为单位)
parameter:{},//参数对象
onReady:function(){},//上传准备时执行
onFinish:function(){},//上传完成时执行
onStop:function(){},//上传停止时执行
onTimeout:function(){}//上传超时时执行
};
return$$.extend(this.options,options||{});
},
//上传文件
upload:function(){
//停止上一次上传
this.stop();
//没有文件返回
if(!this.file||!this.file.value)return;
//可能在onReady中修改相关属性所以放前面
this.onReady();
//设置iframe,form和表单控件
this._setIframe();
this._setForm();
this._setInput();
//设置超时
if(this.timeout>0){
this._timer=setTimeout($$F.bind(this._timeout,this),this.timeout*1000);
}
//开始上传
this._form.submit();
this._sending=true;
},
//设置iframe
_setIframe:function(){
if(!this._iframe){
//创建iframe
variframename="QUICKUPLOAD_"+QuickUpload._counter++,
iframe=document.createElement($$B.ie?"<iframename=""+iframename+"">":"iframe");
iframe.name=iframename;
iframe.style.display="none";
//记录完成程序方便移除
varfinish=this._fFINISH=$$F.bind(this._finish,this);
//iframe加载完后执行完成程序
if($$B.ie){
iframe.attachEvent("onload",finish);
}else{
iframe.onload=$$B.opera?function(){this.onload=finish;}:finish;
}
//插入body
varbody=document.body;body.insertBefore(iframe,body.childNodes[0]);

this._iframe=iframe;
}
},
//设置form
_setForm:function(){
if(!this._form){
varform=document.createElement('form'),file=this.file;
//设置属性
$$.extend(form,{
target:this._iframe.name,method:"post",encoding:"multipart/form-data"
});
//设置样式
$$D.setStyle(form,{
padding:0,margin:0,border:0,
backgroundColor:"transparent",display:"inline"
});
//提交前去掉form
file.form&&$$E.addEvent(file.form,"submit",$$F.bind(this.dispose,this));
//插入form
file.parentNode.insertBefore(form,file).appendChild(file);

this._form=form;
}
//action可能会修改
this._form.action=this.action;
},
//设置input
_setInput:function(){
varform=this._form,oldInputs=this._inputs,newInputs={},name;
//设置input
for(nameinthis.parameter){
varinput=form[name];
if(!input){
//如果没有对应input新建一个
input=document.createElement("input");
input.name=name;input.type="hidden";
form.appendChild(input);
}
input.value=this.parameter[name];
//记录当前input
newInputs[name]=input;
//删除已有记录
deleteoldInputs[name];
}
//移除无用input
for(nameinoldInputs){form.removeChild(oldInputs[name]);}
//保存当前input
this._inputs=newInputs;
},
//停止上传
stop:function(){
if(this._sending){
this._sending=false;
clearTimeout(this._timer);
//重置iframe
if($$B.opera){//opera通过设置src会有问题
this._removeIframe();
}else{
this._iframe.src="";
}
this.onStop();
}
},
//销毁程序
dispose:function(){
this._sending=false;
clearTimeout(this._timer);
//清除iframe
if($$B.firefox){
setTimeout($$F.bind(this._removeIframe,this),0);
}else{
this._removeIframe();
}
//清除form
this._removeForm();
//清除dom关联
this._inputs=this._fFINISH=this.file=null;
},
//清除iframe
_removeIframe:function(){
if(this._iframe){
variframe=this._iframe;
$$B.ie?iframe.detachEvent("onload",this._fFINISH):(iframe.onload=null);
document.body.removeChild(iframe);this._iframe=null;
}
},
//清除form
_removeForm:function(){
if(this._form){
varform=this._form,parent=form.parentNode;
if(parent){
parent.insertBefore(this.file,form);parent.removeChild(form);
}
this._form=this._inputs=null;
}
},
//超时函数
_timeout:function(){
if(this._sending){this._sending=false;this.stop();this.onTimeout();}
},
//完成函数
_finish:function(){
if(this._sending){this._sending=false;this.onFinish(this._iframe);}
}
}

热点内容
支持ftp的免费空间 发布:2025-02-05 16:32:00 浏览:888
python时间比较 发布:2025-02-05 16:31:46 浏览:49
手机银行的密码怎么改密码忘了怎么办啊 发布:2025-02-05 16:02:02 浏览:179
算法牛人左 发布:2025-02-05 15:31:02 浏览:439
php筛选功能 发布:2025-02-05 15:29:09 浏览:168
ip匹配服务器 发布:2025-02-05 15:10:35 浏览:909
php语法后 发布:2025-02-05 15:10:34 浏览:59
oppor9s怎么压缩文件 发布:2025-02-05 15:00:34 浏览:639
苹果耳塞怎么改安卓也能用 发布:2025-02-05 14:50:54 浏览:558
安卓如何鉴别手机真假 发布:2025-02-05 14:28:15 浏览:121