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

所属分类: 网络编程 / ASP.NET 阅读数: 220
收藏 0 赞 0 分享

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

前台代码: 

复制代码 代码如下:

/*修改头像*/ 
    //上传 
    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(); 
            } 
        } 
    } 

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

更多精彩内容其他人还在看

asp.net 页面间传值与跳转的区别

通过Server.Transfer("b.aspx") 与Response.Redirect("b.aspx")的区别
收藏 0 赞 0 分享

ASP.NET Gridview与checkbox全选、全不选实现代码

ASP.NET Gridview checkbox全选与全不选实现代码,其实原理就是利用js来实现的,但需要简单的设置下回传。
收藏 0 赞 0 分享

ASP.NET DropDownList控件的使用方法

ASP.NET DropDownList控件的使用方法,学习asp.net的朋友没用过这个控件的朋友可以参考下。
收藏 0 赞 0 分享

一些.NET对多线程异常处理技巧分享

多线程应用,在实际的项目或产品开发中,原则上来说,应该尽量避免,但是在强调用户体验的要求下或开发平台的限制下(如 Silverlight Socket 通讯),我们不得不用多线程。
收藏 0 赞 0 分享

ASP.NET MVC运行出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null的解决方法

同一相站点,有些页面的客户端验证能工作,而有些死活不行。打开页面就出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null错误
收藏 0 赞 0 分享

asp.net 通用分页显示辅助类(改进版)

在使用ASP.NET编程时,如果不用控件的默认分页功能,想自己搞一个,可以看看本文的asp.net通用分页显示辅助类哦。
收藏 0 赞 0 分享

微软 Visual Studio 2010官方下载地址给大家

昨天VS2010在网上报道都已经发布了,现在今天在网上找到Visual Studio 2010官方下载地址,提供给大家下载。
收藏 0 赞 0 分享

Javascript 直接调用服务器C#代码 ASP.NET Ajax实例

近来总有一些朋友会问到一些入门的问题,把这些问题整理一下,写出来。在以前的文章里,曾经利用纯JS编写过Ajax引擎,在真正开发的时候,大家都不喜欢以这种低效率的方式开发,利用MS Ajax的集成的引擎,可以简单不少工作。
收藏 0 赞 0 分享

ASP.NET 页面刷新的实现方法(包括html,js)

ASP.NET 页面刷新的实现方法,比较全了, 包括html与js下的实现方法。
收藏 0 赞 0 分享

asp.net 无刷新翻页就是这么简单

前两天看了一个自定义分页控件,和AspNetPager一样是实现IPostBackEventHandler接口,不过简洁许多,就想能不能实现ICallbackEventHandler接口做到无刷新分页呢?想到了就马上去做,终于,设想变成了现实!!
收藏 0 赞 0 分享
查看更多