1.官网下载开发包:http://www.uploadify.com/download/,选择免费的Flash版本:
2.解压后,需要用到以下几个文件:
需要修改uploadify.css中取消上传按钮的背景图片路径:
.uploadify-queue-item .cancel a {
background: url('../img/uploadify-cancel.png') 0 0 no-repeat;
float: right;
height: 16px;
text-indent: -9999px;
width: 16px;
}
3.页面添加样式表和脚本库的引用:
<link href="~/Content/uploadify/uploadify.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Content/uploadify/jquery.uploadify.min.js"></script>
4.页面添加用于生成上传按钮的标签:
<span id="btn_upload"></span>
5.初始化,生成按钮:
1 $(function () {
2 $('#btn_upload').uploadify({
3 uploader: '/article/upload', // 服务器处理地址
4 swf: '/Content/uploadify/uploadify.swf',
5 buttonText: "选择文件", //按钮文字
6 height: 34, //按钮高度
7 width: 82, //按钮宽度
8 fileTypeExts: "*.jpg;*.png;", //允许的文件类型
9 fileTypeDesc: "请选择图片文件", //文件说明
10 formData: { "imgType": "normal" }, //提交给服务器端的参数
11 onUploadSuccess: function (file, data, response) { //一个文件上传成功后的响应事件处理
12 var data = $.parseJSON(data);
13 alert(data.imgpath);
14 }
15 });
16 });
6.后台代码:
1 public ActionResult Upload(HttpPostedFileBase Filedata)
2 {
3 // 没有文件上传,直接返回
4 if (Filedata == null || string.IsNullOrEmpty(Filedata.FileName) || Filedata.ContentLength == 0)
5 {
6 return HttpNotFound();
7 }
8
9 //获取文件完整文件名(包含绝对路径)
10 //文件存放路径格式:/files/upload/{日期}/{md5}.{后缀名}
11 //例如:/files/upload/20130913/43CA215D947F8C1F1DDFCED383C4D706.jpg
12 string fileMD5 = CommonFuncs.GetStreamMD5(Filedata.InputStream);
13 string FileEextension = Path.GetExtension(Filedata.FileName);
14 string uploadDate = DateTime.Now.ToString("yyyyMMdd");
15
16 string imgType = Request["imgType"];
17 string virtualPath = "/";
18 if (imgType == "normal")
19 {
20 virtualPath = string.Format("~/files/upload/{0}/{1}{2}", uploadDate, fileMD5, FileEextension);
21 }
22 else
23 {
24 virtualPath = string.Format("~/files/upload2/{0}/{1}{2}", uploadDate, fileMD5, FileEextension);
25 }
26 string fullFileName = this.Server.MapPath(virtualPath);
27
28 //创建文件夹,保存文件
29 string path = Path.GetDirectoryName(fullFileName);
30 Directory.CreateDirectory(path);
31 if (!System.IO.File.Exists(fullFileName))
32 {
33 Filedata.SaveAs(fullFileName);
34 }
35
36 var data = new { imgtype = imgType, imgpath = virtualPath.Remove(0, 1) };
37 return Json(data, JsonRequestBehavior.AllowGet);
38 }
39 }
7.相关参数说明:
onUploadStart:function(file){
$("#btn_upload").uploadify("settings", "formData", { "imgType": "other","imgMode":"big") });
}
8.效果演示:
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有