ASP.NET File Upload with *Real-Time* Progress Bar

简介: After sufficiently embarrassing myself with my earlier post, I decided it was high time I spend a little bit more time understanding this problem.

After sufficiently embarrassing myself with my earlier post, I decided it was high time I spend a little bit more time understanding this problem.  And lo and behold Jon Galloway has already written up a nice survey on the topic.  So to bone up on the issues I followed all of the links Jon provided as well the ones provided by all of the one-off emails I received in response to my previous post (thanks everyone).

Of all the upload components I checked-out (SWFUpload, FileUp, Flajaxian FileUploader, RadUpload, NeatUpload, ASP.Net File Upload/Download Module, etc...) I was the most interested in the last 2 (NeatUpload and ASP.Net File Upload/Download Module).  They are both free, have my kind of licensing and come with the full source code.

So I downloaded both of these components and played around with plugging them into the demo app I blogged about previously.  Both components include progress bar web controls, but I already have a progress bar widget that I really like.  So my focus was on seeing what it would take to leverage only the HttpModule portion of these components and checking out how I could have the module interface with my progress bar.  Like I mentioned previously, I want something that looks like this ...

image

image

image

 

NeatUpload Vs. Darren Johnstone's ASP.Net File Upload/Download Module

Both NeatUpload and Darren Johnstone's components seem to have a rather large following, but I chose to see how I could extend Darren's control because it was already using AJAX plus a custom HttpHandler to communication the upload progress back to the client.  And that seems really close to what I wanted.  If this doesn't pan out, NeatUpload actually seems to have a larger following so I will probably go back and revisit this topic using NeatUpload instead.

 

How it Works

From what I can tell, Darren's component basically works by injecting a hidden INPUT element into the form that contains the file INPUT(s).  The value of the hidden element is a Guid that contains a well-known prefix (Darren uses the token ::DJ_UPLOAD_ID::).  Then as his custom HttpModule is processing the request, it looks to see if the form contains this token.  If so, he tucks the upload progress information into memory using the id value as the index.  Then, if a client wants to query the upload progress, they can query his component using this guid as the look-up value. 

Below is a screen capture of the request body.  The hidden field is highlighted in yellow.       

image  

 

Querying the Upload Status

The second cool thing about Darren's control is that he has included a custom handler for accessing the progress of an upload.  This makes is pretty simple to get at the upload progress using your favorite AJAX library.  Below is a small snippet of JavaScript that I am using in my demo app for getting at the status of the current upload.   

   1: intervalID = window.setInterval(function(){
   2:  
   3:     var request = new Sys.Net.WebRequest();
   4:     request.set_url('UploadProgress.ashx?DJUploadStatus=' + token + '&ts=' + new Date().getTime());
   5:     request.set_httpVerb('GET');                
   6:     request.add_completed(function(executor){
   7:     
   8:         //  the progress is returned as xml
   9:         var e = executor.get_xml().documentElement;                   
  10:         var empty = e.getAttribute('empty');
  11:     
  12:         if(!empty){
  13:         
  14:             //  extract the attributes I am interested in
  15:             var percent = e.getAttribute('progress');
  16:             var file = e.getAttribute('file');
  17:             var kbs = Math.round(parseInt(e.getAttribute('bytes')) / 1024);    
  18:             var size = Math.round(parseInt(e.getAttribute('size')) / 1024);                        
  19:         
  20:             //  update the progressbar to the new value
  21:             progressBar.set_percentage(percent);
  22:             //  upadte the message
  23:             updateMessage('info', 'Uploading ' + file + ' ... ' + kbs + ' of ' + size + ' KB');
  24:             
  25:             if(percent == 100){
  26:                 //  clear the interval
  27:                 window.clearInterval(intervalID);                        
  28:             }                        
  29:        }                    
  30:     });  
  31:     
  32:     //  invoke the request
  33:     request.invoke();
  34:     
  35: }, 1000);

 

Sample App

No live demo this time, but I do have a sample application.  You can download it here.

 

Conclusion

Well, that's about it.  I am curious, how have you handled file uploads in the web apps you have worked on?  3rd party component?  Something home-grown?  One of the components I mentioned above?

 

版权

作者:灵动生活 郝宪玮

出处:http://www.cnblogs.com/ywqu

如果你认为此文章有用,请点击底端的【推荐】让其他人也了解此文章,

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

相关文章
|
11天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
9天前
|
存储 人工智能 搜索推荐
终身学习型智能体
当前人工智能前沿研究的一个重要方向:构建能够自主学习、调用工具、积累经验的小型智能体(Agent)。 我们可以称这种系统为“终身学习型智能体”或“自适应认知代理”。它的设计理念就是: 不靠庞大的内置知识取胜,而是依靠高效的推理能力 + 动态获取知识的能力 + 经验积累机制。
342 130
|
9天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
本文讲解 Prompt 基本概念与 10 个优化技巧,结合学术分析 AI 应用的需求分析、设计方案,介绍 Spring AI 中 ChatClient 及 Advisors 的使用。
430 130
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
|
3天前
|
存储 安全 前端开发
如何将加密和解密函数应用到实际项目中?
如何将加密和解密函数应用到实际项目中?
201 138
|
9天前
|
人工智能 Java API
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
本文介绍AI大模型的核心概念、分类及开发者学习路径,重点讲解如何选择与接入大模型。项目基于Spring Boot,使用阿里云灵积模型(Qwen-Plus),对比SDK、HTTP、Spring AI和LangChain4j四种接入方式,助力开发者高效构建AI应用。
386 122
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
|
3天前
|
存储 JSON 安全
加密和解密函数的具体实现代码
加密和解密函数的具体实现代码
202 136
|
21天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1356 8
|
8天前
|
监控 JavaScript Java
基于大模型技术的反欺诈知识问答系统
随着互联网与金融科技发展,网络欺诈频发,构建高效反欺诈平台成为迫切需求。本文基于Java、Vue.js、Spring Boot与MySQL技术,设计实现集欺诈识别、宣传教育、用户互动于一体的反欺诈系统,提升公众防范意识,助力企业合规与用户权益保护。