前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【要什么自行车】ASP.NET MVC4笔记02:上传文件 uploadify 组件使用

【要什么自行车】ASP.NET MVC4笔记02:上传文件 uploadify 组件使用

作者头像
阿炬
发布2018-05-11 14:30:26
6260
发布2018-05-11 14:30:26
举报
文章被收录于专栏:阿炬.NET阿炬.NET阿炬.NET

参考:http://www.cnblogs.com/luotaoyeah/p/3321070.html

1、下载 uploadify 组件,copy至 Content文件夹

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

2、View

<script type="text/javascript">
$(function () {
    $('#btn_upload').uploadify({
        uploader: '/home/upload',            // 服务器处理地址
        swf: '/Content/uploadify/uploadify.swf',
        buttonText: "上传文件",                  //按钮文字
        height: 34,                             //按钮高度
        width: 82,                              //按钮宽度
        fileTypeExts: "*.jpg;*.png;",           //允许的文件类型
        fileTypeDesc: "请选择图片文件",           //文件说明
        formData: { "imgType": "normal" }, //提交给服务器端的参数
        onUploadSuccess: function (file, data, response) {   //一个文件上传成功后的响应事件处理
            var data = $.parseJSON(data);
            $("#photo").attr("src",data.imgpath);
            //alert(data.imgpath);
        }
    });
});

</script>

<span id="btn_upload"></span>
<br />
<img id="photo" src="http://www.yxweb.com.cn/images/upphoto.gif" alt="请上传工作照" />

3、Controller

       public ActionResult Upload(HttpPostedFileBase Filedata)
        {
            // 没有文件上传,直接返回
            if (Filedata == null || string.IsNullOrEmpty(Filedata.FileName) || Filedata.ContentLength == 0)
            {
                return HttpNotFound();
            }

            //获取文件完整文件名(包含绝对路径)
            //文件存放路径格式:/files/upload/{日期}/{md5}.{后缀名}
            //例如:/files/upload/20130913/43CA215D947F8C1F1DDFCED383C4D706.jpg
            string fileMD5 = GetStreamMD5(Filedata.InputStream);
            string FileEextension = Path.GetExtension(Filedata.FileName);
            string uploadDate = DateTime.Now.ToString("yyyyMMdd");
    
            string imgType = Request["imgType"];
            string virtualPath = "/";
            if (imgType == "normal")
            {
                virtualPath = string.Format("~/files/upload/{0}/{1}{2}", uploadDate, fileMD5, FileEextension);
            }
            else
            {
                virtualPath = string.Format("~/files/upload2/{0}/{1}{2}", uploadDate, fileMD5, FileEextension);
            }
            string fullFileName = this.Server.MapPath(virtualPath);

            //创建文件夹,保存文件
            string path = Path.GetDirectoryName(fullFileName);
            Directory.CreateDirectory(path);
            if (!System.IO.File.Exists(fullFileName))
            {
                Filedata.SaveAs(fullFileName);
            }

            var data = new { imgtype = imgType, imgpath = virtualPath.Remove(0, 1) };
            return Json(data, JsonRequestBehavior.AllowGet);
            }



        /// <summary>
        /// 计算文件流的md5值
        /// </summary>
        /// <param name="stream">文件输入流</param>
        /// <returns></returns>
        public static String GetStreamMD5(Stream stream)
        {
            MD5 md5Hasher = new MD5CryptoServiceProvider();
            /*计算指定Stream对象的哈希值*/
            byte[] arrbytHashValue = md5Hasher.ComputeHash(stream);
            /*由以连字符分隔的十六进制对构成的String,其中每一对表示value中对应的元素;例如“F-2C-4A”*/
            string strHashData = System.BitConverter.ToString(arrbytHashValue).Replace("-", "");
            return strHashData;
        }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-11-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、下载 uploadify 组件,copy至 Content文件夹
  • 2、View
  • 3、Controller
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档