.net文件上传的方法
前台js代码 导入jQuery上传插件Uploadify 3.2
1 $("#photofile").uploadify({//上传电子相片 2 swf: ‘/Theme/NewBlueVacation/images/uploadify.swf‘, 3 uploader: ‘/ExtendClass/UploadFiles.ashx‘, //后台处理文件的路径 4 width: 85, 5 height: 21, 6 buttonText: ‘ ‘,//上传按钮文字 7 fileSizeLimit: ‘20KB‘, 8 multi: false, 9 buttonImage: "/Theme/NewBlueVacation/images/ManualRecharge_tubiao06.png",//上传按钮路径 10 fileTypeExts: ‘*.jpg‘,//;*.png;*.gif;*.bmp;*.doc;*.docx 11 fileTypeDesc: ‘图片文件‘, 12 onSelectError: function (file, errorCode, errorMsg) { 13 switch (errorCode) { 14 case -100: 15 layer.alert("上传的文件数量已经超出系统限制的" + $(‘#photofile‘).uploadify(‘settings‘, ‘queueSizeLimit‘) + "个文件!"); 16 break; 17 case -110: 18 layer.alert("文件 [" + file.name + "] 大小超出系统限制的" + $(‘#photofile‘).uploadify(‘settings‘, ‘fileSizeLimit‘) + "大小!"); 19 break; 20 case -120: 21 layer.alert("文件 [" + file.name + "] 大小异常!"); 22 break; 23 case -130: 24 layer.alert("文件 [" + file.name + "] 类型不正确!"); 25 break; 26 } 27 }, 28 onUploadSuccess: function (file, data, response) { 29 if (data == "0") { 30 $.messager.alert("提示", "上传失败!", ‘error‘); 31 } 32 else { 33 $("<img/>").attr("src", data).load(function () { 34 /* 35 如果要获取图片的真实的宽度和高度有三点必须注意 36 1、需要创建一个image对象:如这里的$("<img/>") 37 2、指定图片的src路径 38 3、一定要在图片加载完成后执行如.load()函数里执行 39 */ 40 realWidth = this.width; 41 realHeight = this.height; 42 //如果真实的宽度大于浏览器的宽度就按照100%显示 43 if (realWidth != 358) { 44 this.width = 358; 45 } 46 else {//如果小于浏览器的宽度按照原尺寸显示 47 //$(img).css("width", realWidth + ‘px‘).css("height", realHeight + ‘px‘); 48 } 49 if (realHeight != 441) 50 { 51 this.height=441; 52 } 53 }); 54 //$(‘#photopic‘).attr(‘src‘, data); 55 $(‘#photofilename‘).val(data); 56 $(‘#phototext‘).val(file.name); 57 } 58 } 59 });
后台C#代码
1 public void ProcessRequest(HttpContext context) 2 { 3 context.Response.ContentType = "text/plain"; 4 HttpPostedFile file = context.Request.Files["Filedata"]; 5 string Dir = "Upload"; 6 string uploadPath = "\\" + Dir + "\\TravelProduct\\" + DateTime.Now.ToString("yyyyMMdd") + "\\"; 7 string returnPath = uploadPath.Replace("\\", "/"); 8 uploadPath = HttpContext.Current.Server.MapPath(uploadPath); 9 if (file != null) 10 { 11 if (!Directory.Exists(uploadPath)) 12 { 13 Directory.CreateDirectory(uploadPath); 14 } 15 string fileType = Tool.MakeRandomNumber(8, 1) + Tool.GetPicType(file.FileName); 16 file.SaveAs(uploadPath + fileType); 17 //下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失 18 context.Response.Write(returnPath + fileType); 19 } 20 else 21 { 22 context.Response.Write("0"); 23 } 24 } 25 #region 取指定文件的后缀名 26 /// <summary> 27 /// 取指定文件的后缀名 28 /// </summary> 29 /// <param name="PicName">文件名或文件路径</param> 30 /// <returns></returns> 31 public static string GetPicType(string PicName) 32 { 33 return Path.GetExtension(PicName); 34 } 35 #endregion
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。