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

上传进度条代码

发布时间: 2025-01-14 03:32:01

⑴ 怎样实现在android实现带进度条的上传效果

实现在android实现带进度条的上传效果效果如图:


用到以下两个类就可实现带进度条的文件上传:


1、CustomMultiPartEntity extends MultipartEntity,


2、HttpMultipartPost extends AsyncTask


代码如下:


import java.io.FilterOutputStream;


import java.io.IOException;


import java.io.OutputStream;


import java.nio.charset.Charset;


import org.apache.http.entity.mime.HttpMultipartMode;


import org.apache.http.entity.mime.MultipartEntity;



public class CustomMultipartEntity extends MultipartEntity {


private final ProgressListener listener;


public CustomMultipartEntity(final ProgressListener listener) {


super();


this.listener = listener;


}


public CustomMultipartEntity(final HttpMultipartMode mode, final ProgressListener listener) {


super(mode);


this.listener = listener;


}


public CustomMultipartEntity(HttpMultipartMode mode, final String boundary,


final Charset charset, final ProgressListener listener) {


super(mode, boundary, charset);


this.listener = listener;


}


@Override


public void writeTo(final OutputStream outstream) throws IOException {


super.writeTo(new CountingOutputStream(outstream, this.listener));


}


public static interface ProgressListener {


void transferred(long num);


}



public static class CountingOutputStream extends FilterOutputStream {


private final ProgressListener listener;


private long transferred;


public CountingOutputStream(final OutputStream out, final ProgressListener listener) {


super(out);


this.listener = listener;


this.transferred = 0;


}


public void write(byte[] b, int off, int len) throws IOException {


out.write(b, off, len);


this.transferred += len;


this.listener.transferred(this.transferred);


}


public void write(int b) throws IOException {


out.write(b);


this.transferred++;


this.listener.transferred(this.transferred);


}


}


}


该类计算写入的字节数,我们需要在实现ProgressListener中的trasnfered()方法,更行进度条



public class HttpMultipartPost extends AsyncTask<HttpResponse, Integer, TypeUploadImage> {



ProgressDialogpd;



longtotalSize;



@Override


protectedvoidonPreExecute(){


pd= newProgressDialog(this);


pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);


pd.setMessage("Uploading Picture...");


pd.setCancelable(false);


pd.show();


}



@Override


(HttpResponse... arg0) {


HttpClienthttpClient = newDefaultHttpClient();


HttpContexthttpContext = newBasicHttpContext();


HttpPosthttpPost = newHttpPost("http://herpderp.com/UploadImage.php");



try{


= newCustomMultipartEntity(


newProgressListener() {



@Override


public void transferred(longnum){


publishProgress((int) ((num / (float) totalSize) * 100));


}


});



// We use FileBody to transfer an image


multipartContent.addPart("uploaded_file", newFileBody(


newFile(m_userSelectedImagePath)));


totalSize= multipartContent.getContentLength();



// Send it


httpPost.setEntity(multipartContent);


HttpResponseresponse = httpClient.execute(httpPost, httpContext);


String serverResponse = EntityUtils.toString(response.getEntity());



ResponseFactoryrp = newResponseFactory(serverResponse);


return(TypeImage) rp.getData();


}



catch(Exception e) {


System.out.println(e);


}


returnnull;


}



@Override


protectedvoidonProgressUpdate(Integer... progress){


pd.setProgress((int) (progress[0]));


}



@Override


protectedvoidonPostExecute(TypeUploadImageui) {


pd.dismiss();


}


}

在 transferred()函数中调用publishProgress((int) ((num / (float) totalSize) * 100));


在onProgressUpdate()实现上传进度的更新操作

⑵ asp 如何实现带进度条的上传文件功能

以下就以abcupload4为例来说明怎么来制作实时的文件上传进度条。

(注:我们在abcupload自带例子基础上改进。)

progressupload.htm(上传文件的前台提交,我们让进度条在这个里面显示)

<HTML>

<body>

<script language="javascript">

<!--

theUniqueID = (new Date()).getTime() % 1000000000;

function s() //让数据提交的同时执行显示进度条的函数

{

bar(); //开始执行反映上传情况的函数

document.myform.action = "progressupload.ASP?ID=" theUniqueID; //处理上传数据的程序

document.myform.target="up" //将提交的数据放在一个名字是up隐藏的iframe里面处理,这样提交的页面就不会跳转到处理数据的页

document.myform.submit(); //提交表单

}

function bar()

{

bar1.style.display=''; //让显示上传进度显示的层的可见

var timeoutid=null; //这个变量是作定时器的ID

var oXMLDoc = new ActiveXObject('MSXML'); //创建'MSXML'对象

sURL = "progressbar.ASP?ID=" theUniqueID "&temp=" Math.random(); //获取上传状态数据的地址

oXMLDoc.url = sURL; //load数据

var oRoot=oXMLDoc.root; //获取返回XML数据的根节点

if(oRoot.children != null)

{

if (oRoot.children.item(0).text-100==0) //文件上传结束就取消定时器

clearTimeout(timeoutid)

PercentDone.style.width=oRoot.children.item(0).text "%"; //设置进度条的百分比例

//根据返回的数据在客户端显示

min.innerHTML=oRoot.children.item(1).text; //显示剩余时间(分钟)

secs.innerHTML=oRoot.children.item(2).text; //显示剩余时间(秒钟)

BytesDone.innerHTML=oRoot.children.item(3).text; //已上传数据大小

BytesTotal.innerHTML=oRoot.children.item(4).text; //总大小

BytesPerSecond.innerHTML=oRoot.children.item(5).text; //传输速率

Information.innerHTML=oRoot.children.item(6).text; //上传信息

}

if (oRoot.children.item(0).text-100<0) //只要文件没有传完,就每隔多少时间获取一次数据

timeoutid = setTimeout("bar()",50) //这里设定时间间隔是0.05秒,你也可以根据你的情况修改获取数据时间间隔

}

//-->

</script>

<form name="myform" method="post" action="progressupload.ASP" enctype="multipart/form-data" target=up>

<input type="file" name="filefield1"><br>

<input type="button" name="dosubmit" value="Upload" onclick="s()"><br>

<div id=bar1 style="display:none">

<table border="0" width="100%">

<tr>

<td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>传送:</b></font></td>

</tr>

<tr bgcolor="#999999">

<td>

<table border="0" width="" cellspacing="1" bgcolor="#0033FF" id=PercentDone>

<tr>

<td><font size=1></font></td>

</tr>

</table>

</td>

</tr>

<tr>

<td>

<table border="0" width="100%">

<tr>

<td><font face="Verdana, Arial, Helvetica, sans-serif" size="1">剩余时间:</font></td>

<td><font face="Verdana, Arial, Helvetica, sans-serif" size="1">

<span id=min></span> 分

<span id=secs></span> 秒

(<span id=BytesDone></span> KB of

<span id=BytesTotal></span> KB 已上传)</font></td>

</tr>

<tr>

<td><font face="Verdana, Arial, Helvetica, sans-serif" size="1">

传送速度:</font></td>

<td><font face="Verdana, Arial, Helvetica, sans-serif" size="1">

<span id=BytesPerSecond></span> KB/秒</font></td>

</tr>

<tr>

<td><font face="Verdana, Arial, Helvetica, sans-serif" size="1">信息:</font></td>

<td><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><span id=Information></span></font></td>

</tr>

</table>

</td>

</tr>

<tr></tr>

</table>

</div>

<iframe name="up" style="display:none"></iframe>

</form>

</body>

</HTML>

progressbar.ASP(返回上传状况数据的文件)

<%@EnableSessionState=False%>

<%

On Error Resume Next

Set theProgress = Server.CreateObject("ABCUpload4.XProgress") '创建上传组件对象

theProgress.ID = Request.QueryString("ID")

'将返回数据以XML格式输出

%>

<?XML version="1.0" encoding="gb2312" ?>

<plan>

<PercentDone><%=theProgress.PercentDone%></PercentDone>

<min><%=Int(theProgress.SecondsLeft/60)%></min>

<secs><%=theProgress.SecondsLeft Mod 60%></secs>

<BytesDone><%=Round(theProgress.BytesDone / 1024, 1)%></BytesDone>

<BytesTotal><%=Round(theProgress.BytesTotal / 1024, 1)%></BytesTotal>

<BytesPerSecond><%=Round(theProgress.BytesPerSecond/1024, 1)%></BytesPerSecond>

<Information><%=theProgress.Note%></Information>

</plan>

progressupload.ASP(处理上传文件)

<%@EnableSessionState=False%>

<%

Response.Expires = -10000

Server.ScriptTimeOut = 300

Set theForm = Server.CreateObject("ABCUpload4.XForm")

theForm.Overwrite = True

theForm.MaxUploadSize = 8000000

theForm.ID = Request.QueryString("ID")

Set theField = theForm("filefield1")(1)

If theField.FileExists Then

theField.Save theField.FileName

End If

%>

<HTML>

<body>

传送结束

</body>

</HTML>

⑶ c#中,上传文件需要进度条,应该怎样使用

c#有一个progressBar控件就是进度条,开线程,线程函数中加循环,循环执行progressBar1.Value=(int)((已上传大小/总大小)*100)来显示当前进度。

⑷ 使用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)]

更多用法可以 参考官网

热点内容
手机怎么控制服务器 发布:2025-01-14 19:58:46 浏览:306
php难招 发布:2025-01-14 19:06:07 浏览:489
sublime编译php 发布:2025-01-14 18:57:16 浏览:307
云计算服务器是什么 发布:2025-01-14 18:56:22 浏览:44
vip域名查询ftp 发布:2025-01-14 18:46:48 浏览:116
格式化linux 发布:2025-01-14 18:35:14 浏览:595
如何进入安卓原生市场 发布:2025-01-14 18:22:06 浏览:560
台式电脑找不到服务器 发布:2025-01-14 18:19:58 浏览:423
androidsdk网盘 发布:2025-01-14 18:17:43 浏览:82
个别用户访问不了腾讯云服务器 发布:2025-01-14 18:03:27 浏览:279