首页 > 其他 > 详细

uploadify的简单使用

时间:2018-07-02 15:59:36      阅读:166      评论:0      收藏:0      [点我收藏+]
简单的图片上传:
  1.进入官网下载uploadify插件:http://www.uploadify.com/download/
  2.导入uploadify插件提供的css样式和类库: 
 <link href="~/Content/uploadify/uploadify.css" rel="stylesheet" />
 <script src="~/Content/uploadify/jquery.uploadify.min.js"></script>

  3.js页面

var auth = "@(Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value)";
var ASPSESSID = "@Session.SessionID";
$("#j_btn").uploadify({
   buttonText: ‘上传图片‘,
   height: 20,
   width: 120,
   swf: ‘/Content/uploadify/uploadify.swf‘,
   uploader: ‘/Test_Areas/Test/Upload‘,//通过后台的程序把文件上传到服务器
   multi: false,//是否允许同时选择多个文件
   fileSizeLimit: ‘8MB‘,//文件大小
   fileTypeExts: ‘*.gif;*.png;*.jpg;*jpeg‘,//可选文件的扩展名
   formData: {
       ‘folder‘: ‘/Upload‘, ‘ASPSESSID‘: ASPSESSID, ‘AUTHID‘: auth//测试
         },
   onUploadSuccess: function (file, data, response) {
      var jsonData = $.parseJSON(data);
      $.procAjaxMsg(jsonData, function () {
          $.alertMsg(jsonData.Msg, ‘操作提示‘, function () {
             addForm.find("#addForm img").attr("src", jsonData.BackUrl);
             //此处得到的是上传路径
          });
      }, function () {
          $.alertMsg(jsonData.Msg, ‘操作提示‘, null);
      });
    },
    onUploadError: function (file, errorCode, errorMsg, errorString) {
       $.alertMsg(‘文件 ‘ + file.name + ‘ 上传失败: ‘ + errorString, ‘上传失败‘, null);
       },
    onSelectError: function (file, errorCode, errorMsg, errorString) {
       $.alertMsg(‘文件 ‘ + file.name + ‘ 不能被上传: ‘ + errorString, ‘选择失效‘, null);
       }
    })

4.Controller页面

技术分享图片
 #region 08-上传图片
        /// <summary>
        /// 08-上传图片
        /// </summary>
        /// <returns></returns>
        public ActionResult Upload()
        {
            try
            {
                if (Request.Files.Count == 0)
                {
                    return PackagingAjaxmsg(new AjaxMsgModel { BackUrl = null, Data = null, Msg = "未找到要上传的图片", Statu = AjaxStatu.err });
                }
                else
                {
                    //得到上传的图片
                    var file = Request.Files[0];
                    //要保存的文件路径
                    var path = Path.Combine(Server.MapPath(Request.ApplicationPath), "Upload", "Image");
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    //要保存的文件名称
                    string fileName = string.Format("{0}_{1}{2}", oc.CurrentUser.uId, DateTime.Now.ToString("yyyyMMddHHmmss"), Path.GetExtension(file.FileName));
                    //保存文件到指定的目录
                    file.SaveAs(Path.Combine(path, fileName));
                    //上传之后图片的相对路径
                    string relativePath = Path.Combine(Request.ApplicationPath, "Upload", "Image", fileName);
                   
                    return PackagingAjaxmsg(new AjaxMsgModel { BackUrl = relativePath, Data = null, Msg = "上传成功", Statu = AjaxStatu.ok });
                }
            }
            catch (Exception ex)
            {
                return PackagingAjaxmsg(new AjaxMsgModel { BackUrl = null, Data = null, Msg = "图片上传失败", Statu = AjaxStatu.err });
            }
        }
        #endregion
上传图片

 

uploadify的简单使用

原文:https://www.cnblogs.com/chenze-Index/p/9254248.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!