欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net+ajaxfileupload.js 实现文件异步上传代码分享

程序员文章站 2024-02-18 08:00:16
由于代码很简单,这里就闲话不多说了,直接上代码,小伙伴们自己研读代码就明白了。 前台代码:  复制代码 代码如下: /*修改头像*/  &nb...

由于代码很简单,这里就闲话不多说了,直接上代码,小伙伴们自己研读代码就明白了。

前台代码: 

复制代码 代码如下:

/*修改头像*/ 
    //上传 
    function _sc() { 
        $(".ckfile").html("").css("color", "#535353"); 
        $("#_userimgpath").val(""); 
        var str = $("#file").val(); 
        if ($.trim(str) == "") { 
            $(".ckfile").html("请选择文件。").css("color", "red"); 
            return false; 
        } 
        else { 
            var postfix = str.substring(str.lastindexof(".") + 1).touppercase(); 
            if (postfix == "jpg" || postfix == "jpeg" || postfix == "png" || postfix == "gif" || postfix == "bmp") { 
                $('#showimg').attr('src', 'images/loading.gif').attr("title", "上传中,请稍后…"); 
                var path = "upload/userimg"; 
                $.ajaxfileupload({ 
                    url: '/upload.aspx?path=upload|userimg&shape=100*100', 
                    secureuri: false, 
                    fileelementid: 'file', 
                    datatype: 'text', 
                    success: function (msg) { 
                        if (msg.lastindexof(path) == -1) { 
                            $(".ckfile").html(msg).css("color", "red"); 
                        } 
                        else { 
                            $('#showimg').attr('src', msg).attr("title", "我的头像"); 
                            $("#_userimgpath").val(msg); 
                        } 
                    } 
                }); 
            } else { 
                $(".ckfile").html("文件格式错误。").css("color", "red"); 
                return false; 
            } 
        } 
    } 

后台代码:

复制代码 代码如下:

using system; 
using system.collections.generic; 
using system.linq; 
using system.web; 
using system.web.ui; 
using system.web.ui.webcontrols; 
using ss.upload; 
using wfc.fenxiao; 
namespace wanfangcheng 

    public partial class upload : basepage 
    { 
        //文件大小 1024 kb 
        private long size = 1024; 
        //文件类型 
        private string type = ".jpg|.jpeg|.png|.gif|.bmp"; 
        //保存名称 
        string name = ""; 
        //保存路径 
        private string path = @"upload/userimg"; 
        //保存大小 
        private string shape = "100*100"; 
        protected void page_load(object sender, eventargs e) 
        { 
            httpfilecollection files = request.files; 
            if (files != null && files.count > 0) 
            { 
                name = baserole.instance.userid.tostring(); 
                if (request.querystring["size"] != null) 
                { 
                    size = convert.toint32(request.querystring["size"]); 
                } 
                if (request.querystring["path"] != null) 
                { 
                    path = request.querystring["path"].tostring().trim().replace('|', '/'); 
                } 
                if (request.querystring["name"] != null) 
                { 
                    name = request.querystring["name"].tostring().trim(); 
                } 
                if (request.querystring["shape"] != null) 
                { 
                    shape = request.querystring["shape"].tostring().trim(); 
                } 
                uploadmethod(files); 
            } 
        } 
        /// <summary> 
        /// 上传图片 
        /// </summary> 
        /// <param name="hc"></param> 
        public void uploadmethod(httpfilecollection hc) 
        { 
            httppostedfile _file = hc[0]; 
            //文件大小 
            long _size = _file.contentlength; 
            if (_size <= 0) 
            { 
                response.write("文件错误。"); 
                response.end(); 
                return; 
            } 
            if (size * 1024 < _size) 
            { 
                response.write("文件过大,最大限制为" + size + "kb。"); 
                response.end(); 
                return; 
            } 
            //文件名 
            string _name = _file.filename; 
            //文件格式 
            string _tp = system.io.path.getextension(_name).tolower(); 
            if (type.indexof(_tp) == -1) 
            { 
                response.write("文件格式错误。"); 
                response.end(); 
                return; 
            } 
            //保存路径 
            string _path = httpcontext.current.server.mappath(path) + @"/" + name + _tp; 
            try 
            { 
                int w = convert.toint32(shape.split('*')[0]); 
                int h = convert.toint32(shape.split('*')[1]); 
                imagehelper.cutforcustom(_file, _path, w, h, 50); 
                response.write(path + @"/" + name + _tp); 
            } 
            catch (exception) 
            { 
                response.write("哎呦,出错了。"); 
                response.end(); 
            } 
        } 
    } 

是不是很实用,也很简单易懂呢,以上是自己项目中使用的代码,小伙伴们如果发现有问题的地方,还请告之。谢谢