异步提交图片

简单总结一下uploadify提交图片方法

js代码:

     $("#headPicture").uploadify({
                height: 32,
                width: 132,
                swf: '/js/uploadify/uploadify.swf',
                uploader: '/user/UpLoadPicture',
                buttonText: '上传头像',          
                formData:{
                    //action:'uploadImg'
                },
                successTimeout: 99999,
                fileSizeLimit: '2MB',
                fileTypeExts:'*.jpg;*.jpge;*.png',                  
                onUploadSuccess: function (file,data,response) {
                    var result = JSON.parse(data);
                    if (result.Result) {                      
                        $("#headPic").attr("src","data:image/"+result.Data.split('|')[0]+";base64,"+result.Data.split('|')[1]);
                    }
                }
            });


html代码:

 <img src="/img/Exchange.png" class="EX_Gold_img fl" id="headPic">
 <input id="headPicture" class="hidden" type="file" name="headPicture" value="">

 

后台c#代码:

        [HttpPost]
        public ActionResult UpLoadPicture( )
        {
           
            ReturnResult result = new ReturnResult() { Result = true };

            if (this.Request.Files.Count > 0)
            {
                var file = this.Request.Files[0];
                List<String> lastNameAlow = new List<String>() { "gif", "jpeg", "png", "jpg" };
                String lastName = file.FileName.Split('.')[file.FileName.Split('.').Length - 1];
                if (lastNameAlow.Exists(a => a == lastName.Trim().ToLower()))
                {
                    byte[] bytes = new byte[file.InputStream.Length];
                    file.InputStream.Read(bytes, 0, bytes.Length);
                    String s = Convert.ToBase64String(bytes);
                    result.Result = true;
                    result.Data = lastName + "|" + s;
                }
            }
            return this.Json(result);
        }


另外需要引用外部css文件、js文件、以及上传用的swf文件

   <link href="/js/uploadify/uploadify.css" rel="stylesheet" />
   <script src="/js/uploadify/jquery.uploadify.js"></script>

swf文件不用动

 

uploadify文件下载链接:  https://files.cnblogs.com/files/xbblogs/uploadify.zip

posted @ 2015-09-06 23:14  .追风逐月  阅读(272)  评论(0编辑  收藏  举报