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

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

 

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

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

<link href=http://www.mamicode.com/"~/Content/uploadify/uploadify.css" rel="stylesheet" /><script src=http://www.mamicode.com/"~/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 =http://www.mamicode.com/ $.parseJSON(data);            $("#photo").attr("src",data.imgpath);            //alert(data.imgpath);        }    });});</script><span id="btn_upload"></span><br /><img id="photo" src=http://www.mamicode.com/"http://www.yxweb.com.cn/images/upphoto.gif" alt="请上传工作照" />

 

3、Controller

       public ActionResult Upload(HttpPostedFileBase Filedata)        {            // 没有文件上传,直接返回            if (Filedata =http://www.mamicode.com/= 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 = http://www.mamicode.com/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 =http://www.mamicode.com/ md5Hasher.ComputeHash(stream);            /*由以连字符分隔的十六进制对构成的String,其中每一对表示value中对应的元素;例如“F-2C-4A”*/            string strHashData = http://www.mamicode.com/System.BitConverter.ToString(arrbytHashValue).Replace("-", "");            return strHashData;        }

 

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