开发者社区> 问答> 正文

如何做上传进度?

由于经常用户上传的时候处于提示:上传中,问加你稍大会员就以为卡住了,请问如何做一个上传进度来显示状态呢?求解!在线等。

展开
收起
ap6865r1d 2012-03-28 11:05:05 11985 0
11 条回答
写回答
取消 提交回答
  • Re如何做上传进度
    Re:如何做上传进度?
    2014-11-26 15:54:38
    赞同 展开评论 打赏
  • Re如何做上传进度
    亲测有效。。。。。
    2014-11-23 16:14:54
    赞同 展开评论 打赏
  • Re如何做上传进度
    .net 可以实现吗
    2013-07-16 13:17:17
    赞同 展开评论 打赏
  • 回5楼enj0y的帖子
    想弄个,从服务器上传到oss的进度显示条
    2013-07-12 18:38:18
    赞同 展开评论 打赏
  • 做传递吧。
    2012-08-20 22:22:00
    赞同 展开评论 打赏
  • 回 5楼(ap6214f2r) 的帖子
    我也好奇,表示为上传过中,客户端浏览器显示的上传进度。(使用AJAX来上传,同时显示进度的除外)
    2012-08-20 20:54:21
    赞同 展开评论 打赏
  • 楼主是想要哪样的进度?
    浏览器->你的服务器->OSS,
    是要哪一部分的。
    2012-08-20 18:31:43
    赞同 展开评论 打赏
  • Re如何做上传进度


    能提供一下比较详细的样例代码吗?
    2012-08-20 18:06:40
    赞同 展开评论 打赏
  • 弄一幅等待的图片,上传的时候显示出来,上传完隐藏,简单了事~
    2012-04-02 22:19:06
    赞同 展开评论 打赏
  • 回 1楼(erd3erd3) 的帖子
    PHP的
    2012-04-01 12:56:29
    赞同 展开评论 打赏
  • 你用的是什么语言开发的?如果是java的话,可以继承一个inputstream在里面实现一些和进度相关的处理,然后将该inputstream传给putObject就可以实现了,下面是简单的代码示例:


    public class ProgressReportingInputStream extends FilterInputStream {

        private static final int NOTIFICATION_THRESHOLD = 8 * Constants.KB;

        private final ProgressListener listener;
        
        private int unnotifiedByteCount;

        public ProgressReportingInputStream(final InputStream in, final ProgressListener listener) {
            super(in);
            this.listener = listener;
        }

        @Override
        public int read() throws IOException {
            int data = super.read();
            if (data != -1) notify(1);
            return data;
        }

        @Override
        public int read(byte[] b, int off, int len) throws IOException {
            int bytesRead = super.read(b, off, len);
            if (bytesRead != -1) notify(bytesRead);
            return bytesRead;
        }

        @Override
        public void close() throws IOException {
            if (unnotifiedByteCount > 0) {
                listener.progressChanged(new ProgressEvent(unnotifiedByteCount));
                unnotifiedByteCount = 0;
            }
            super.close();
        }
        
        private void notify(int bytesRead) {
            unnotifiedByteCount  = bytesRead;
            if (unnotifiedByteCount >= NOTIFICATION_THRESHOLD) {
                listener.progressChanged(new ProgressEvent(unnotifiedByteCount));
                unnotifiedByteCount = 0;
            }
        }
        
        
    }
    public interface ProgressListener {
        public void progressChanged(ProgressEvent progressEvent);
    }

    public class ProgressEvent {
        public static final int DEFAULT_EVENT_CODE = 0;
        public static final int STARTED_EVENT_CODE   = 1;
        public static final int COMPLETED_EVENT_CODE = 2;
        public static final int FAILED_EVENT_CODE    = 4;
        public static final int CANCELED_EVENT_CODE  = 8;


        private int bytesTransfered;
        
        private int eventCode;

        
        public ProgressEvent(int bytesTransfered) {
            this.bytesTransfered = bytesTransfered;
        }

        public void setBytesTransfered(int bytesTransfered) {
            this.bytesTransfered = bytesTransfered;
        }

        public int getBytesTransfered() {
            return bytesTransfered;
        }

        public int getEventCode() {
            return eventCode;
        }

        public void setEventCode(int eventType) {
            this.eventCode = eventType;
        }

    }
    public class ProgressListenerImp implements ProgressListener {
        private final int SPEED_UPADATE_INTERVAL = 300;
        private IconTableModel iconTableModel;
        private String taskName;
        private long totalSize = 0;
        private long curSize = 0;
        private long newUploadSize = 0;
        private long lastTime = 0;
        private String taskType;
        private boolean isFailed = false;

        @Override
        public void progressChanged(ProgressEvent progressEvent) {
            switch (progressEvent.getEventCode()) {
            case ProgressEvent.DEFAULT_EVENT_CODE:
                int ratio = -1,
                speed = -1;
                long curTime = new Date().getTime();
                curSize  = progressEvent.getBytesTransfered();
                newUploadSize  = progressEvent.getBytesTransfered();
                if (totalSize > 0) {
                    ratio = (int) (curSize * 100 / totalSize);
                }
                if (curTime - lastTime > SPEED_UPADATE_INTERVAL) {
                    speed = (int) (newUploadSize * 1000 / (curTime - lastTime));
                    lastTime = curTime;
                    newUploadSize = 0;
                }
                if (!isFailed) {
                    iconTableModel.setValueAt(taskName, ratio, taskType, speed);
                }
                break;
            case ProgressEvent.STARTED_EVENT_CODE:
                iconTableModel.setValueAt(taskName, 0, taskType, 0);
                lastTime = new Date().getTime();
                break;
            case ProgressEvent.COMPLETED_EVENT_CODE:
                iconTableModel.setValueAt(taskName, 100, "Completed", 0);
                iconTableModel.removeTask(taskName);
                break;
            case ProgressEvent.CANCELED_EVENT_CODE:
                iconTableModel.setValueAt(taskName, -1, "Canceled", 0);
                break;
            case ProgressEvent.FAILED_EVENT_CODE:
                isFailed = true;
                iconTableModel.setValueAt(taskName, -1, "Failed", 0);
                break;
            }
        }

        public ProgressListenerImp(ResourceManager resourceManager,
                IconTableModel iconTableModel, String taskName, long totalSize,
                int taskType) {
            super();
            this.iconTableModel = iconTableModel;
            this.taskName = taskName;
            this.totalSize = totalSize;
            if (taskType == Utils.CREATE_FOLDER
                    || taskType == Utils.UPLOAD_FILE) {
                this.taskType = "Uploading";
            } else {
                this.taskType = "Downloading";
            }
        }

    }










    2012-03-29 11:33:50
    赞同 展开评论 打赏
滑动查看更多
问答地址:
问答排行榜
最热
最新

相关电子书

更多
十分钟上线-使用函数计算构建支付宝小程序服务 立即下载
优化4K制作流程,创建“4K视觉云”服务 立即下载
如何实现应用的持续发布 立即下载