随笔 - 54  文章 - 1  评论 - 407  阅读 - 15万

ASP.NET Jquery+ajax上传文件(带进度条)

效果图

支持ie6+,chrome,ie6中文文件名会显示乱码.

上传时候会显示进度条.

需要jquery.uploadify.js插件,稍后会给出下载

前台代码

复制代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

    <script src="fileupload/jquery-1.4.4.min.js" type="text/javascript" language="javascript"></script>

<!-- 脚本部分提示修改为中文 -->
    <script src="fileupload/jquery.uploadify.js" type="text/javascript" language="javascript"></script>

    <link href="fileupload/uploadify.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript" language="javascript">
    $(function() {
        $("#file_upload").uploadify({
            'auto': false,                      //是否自动上传
            'swf': 'fileupload/uploadify.swf',      //上传swf控件,不可更改
            'uploader': 'Handler.ashx',            //上传处理页面,可以aspx
            'fileTypeDesc':'pdf文件或者图像',
            'fileTypeExts':'*.pdf;*.jpg;*.jpeg;*.gif;*.png',   //文件类型限制,默认不受限制
            'buttonText':'浏览文件',//按钮文字
            'width'     :100,
            'height'    :26,
            //最大文件数量'uploadLimit':
            'multi'     :false,//单选            
            'fileSizeLimit':'20MB',
            'queueSizeLimit':1,  //队列限制
            'removeCompleted' : false
        });
    });
    </script>

    <title>Jquery文件上传</title>
</head>
<body>
    <form id="form1" runat="server">
        <input type="file" name="file_upload" id="file_upload" />
        <a href="javascript:$('#file_upload').uploadify('cancel')" >删除</a><!-- 删除文件实际操作通过ajax提交. -->
        <a href="javascript:$('#file_upload').uploadify('upload','*')">Upload Files</a>
      
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        api参考:&nbsp;http://www.uploadify.com/documentation/
    </form>
</body>
</html>
复制代码

后台无,

处理ashx页面代码:

复制代码
<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        if (context.Request.Files.Count > 0)
        {
            //HttpContext.Current.Request.FilePath;
            string strPath = System.Web.HttpContext.Current.Server.MapPath("~/upload/");
            string strName = context.Request.Files[0].FileName;
            context.Request.Files[0].SaveAs(System.IO.Path.Combine(strPath, strName));
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}
复制代码

 

 下载地址

http://download.csdn.net/detail/wukaiping870123/6259419

posted on   火星大能猫  阅读(8508)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示