显示文件上传过程中的进度条 [英] showing a progress bar during file upload

查看:216
本文介绍了显示文件上传过程中的进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应该文件上传时显示进度的异步任务。一切正常,除了它看起来像它完成文件上传真的非常快,然后它只是在100%等待坐在那里。

I've got an async task that is supposed to show progress during a file upload. Everything is working except that it looks like it finishes the file upload really really fast, and then it just sits there at 100% waiting.

我跟踪下来到

URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();

// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);

// Enable POST method
connection.setRequestMethod("POST");

connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"Filedata\";filename=\"" + pathToOurFile + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
long totalBytesWritten = 0;
while (bytesRead > 0) {
    outputStream.write(buffer, 0, bufferSize);
    outputStream.flush();
    if (mCancel) { throw new CancelException(); }

    totalBytesWritten += bufferSize;
    if (mProgressDialog != null) { 
            mProgressDialog.setProgress(Integer.valueOf((int) (totalBytesWritten / 1024L))); 
    }

    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();

我注意到的是,有没有真正的延迟,直到它的得到响应code中的最后一行。我认为发生的事情是该数据得到缓冲,所以它看起来像它已经上传了,但还没有真正 - 它刚刚缓冲它。然后,当我到达GETRESPONSE code()调用,它没有选择,只能完成上传来获取上传状态。有没有什么办法,使其真正沿途上传,所以我可以得到一个合理的进步吗?

What I noticed is that there's no real delay until the very last line where it's getting the response code. I think what's happening is that the data is getting buffered so it looks like it has uploaded it, but hasn't really - it has just buffered it. Then when i get to the getResponseCode() call, it has no choice but to finish the upload to get the upload status. is there any way to make it actually upload along the way so i can get a reasonable progress?

推荐答案

这是邮政是如何设计的HTTP工作,所以不要指望它给你进步的详细信息。

This is how HTTP Post is designed to work so do not expect it to give you progress details.

您可以使用市面上的几个文件上传组件之一。他们在内部使用Flash或Silverlight或iFrame显示进度。

You can use one of the several file uploader components available in the market. They internally use flash or silverlight or iframes to show the progress.

http://dhtmlx.com/docs/products/dhtmlxVault/index.shtml

http://www.element-it.com/多文件上传/闪存uploader.aspx

您会发现许多这样的人,如果你google了一下。

You will find many such others if you google a bit.

他们在内部使用原始IO而不是HTTP POST来处理多个文件和进度通知。雅虎和谷歌也使用这种技术制作附件的邮件。

They internally use raw IO instead of http post to handle multiple files and progress notification. Yahoo and Google also uses such techniques for making attachments to mail.

如果你真的喜欢冒险的感觉,你可以重新创建轮 - 也就是写自己的组件

If you are really feeling adventurous, you can recreate the wheel - i.e. write your own component.

编辑:

请注明,如果你想这样做在Windows桌面应用程序或Web应用程序。

Please specify if you want to do this in a windows desktop application or a web application.

这篇关于显示文件上传过程中的进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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