一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

uploadify+asp.net(C#) 批量上传大文件实例源码

时间:2015-08-14 编辑:简简单单 来源:一聚教程网

最近项目需求要批量上传大文件,项目是用asp.net(C#)开发的,uploadify上传组件个人认为非常优秀,支持asp.net、php等,本文我们就用uploadify实现批量上传大文件。


效果图:


前端代码带注释说明:



    
    
    css/style.css" rel="stylesheet" type="text/css" />
    
    javascript">
    
    
        $(function () {
            $("#uploadify").uploadify({
                //指定swf文件
                'swf': 'uploadify/uploadify.swf',
                //后台处理的页面
                'uploader': 'uploadfile.ashx',
                //按钮显示的文字
                'buttonText': '浏 览',
                //上传文件的类型  默认为所有文件    'All Files'  ;  '*.*'
                //在浏览窗口底部的文件类型下拉菜单中显示的文本
                'fileTypeDesc': 'Image Files',
                //允许上传的文件后缀
                'fileTypeExts': '*.gif; *.jpg; *.png;*.zip',
                //发送给后台的其他参数通过formData指定
                //'formData': { 'someKey': 'someValue', 'someOtherKey': 1 },
                //上传文件页面中,你想要用来作为文件队列的元素的id, 默认为false  自动生成,  不带#
                //'queueID': 'fileQueue',
                'fileSizeLimit': 204800000,
                //选择文件后自动上传
                'auto': false,
                //设置为true将允许多文件上传
                'multi': true,
                //上传成功
                'onUploadSuccess': function (file, data, response) {
                    var obj = (new Function("return " + data))();//【json字符串转为json对象。】
                    $("#rep").append("" + obj.Msg + "!");//data为后台返回结果。
                }
            });
        });
    


    
        
                         
                         

                上传|              取消上传             

        
                 返回的结果:
    


启用批量上传:

javascript:$('#uploadify').uploadify('upload','*'):启用批量上传。

关于大文件上传

在调试上传过程中,发现大文件(大于20M)就出现500错误了,服务器配置是可以上传500M的文件。我就想起应该是webconfig的请求内容大小的限制问题。应该按照如下设置:

设置请求数据大小。


    
    
    


服务器端代码如下:

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    //接收上传后的文件
    HttpPostedFile file = context.Request.Files["Filedata"];
    //其他参数
    //string somekey = context.Request["someKey"];
    //string other = context.Request["someOtherKey"];
    //获取文件的保存路径
    string uploadPath =
        HttpContext.Current.Server.MapPath("UploadImages" + "\\");
    //判断上传的文件是否为空
    if (file != null)
    {
        if (!Directory.Exists(uploadPath))
        {
            Directory.CreateDirectory(uploadPath);
        }
        //保存文件
        file.SaveAs(uploadPath + DateTime.Now.ToString("yyyyMMddHHmmsss") + file.FileName.Substring(file.FileName.LastIndexOf(".") - 1));
        ResponseModel rm = new ResponseModel();
        rm.Id = 1;
        rm.state = 0;
        rm.Msg = "成功";
        context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(rm));//即传给前台的data
    }
    else
    {
        context.Response.Write("0"); //即传给前台的data
    }
}
public class ResponseModel
{
    public int Id { get; set; }
    public int state { get; set; }
    public string Msg { get; set; }
}


其中上传成功后的返回对象可采用json序列化。然后返回给客户端调用。而在客户端调用的时候,建议先给返回的json字符串转换为json对象,这样可以方便使用。

如何处理上传结果返回的数据:

var obj = (new Function("return " + data))();//data为返回的json字符串


热门栏目