前台JS代码
<script> $(function(){ //行程图片上传 $(".info-r input[type='file']").uploadify({ formData: { //这里可以传一些其它参数到后台去。 folder: '\Upload\TravelProduct\King' //图片保存的路径,如果没有前面//,图片就会保存到后台处理的一个路径下面去,如有保存路径为D:webUploadTravelProductKing/图片名称;如没有路径为D:/web/ExtemdClass/Upload/TravelProduct/king/图片名称 }, swf: '/Theme/NewBlueVacation/images/uploadify.swf', uploader: '/ExtendClass/UploadFiles.ashx', 50, height: 20, buttonText: ' ',//上传按钮文字 buttonImage: "/Theme/NewBlueVacation/images/btn.gif",//上传按钮路径 fileTypeExts: '*.jpg;*.png;*.gif;*.bmp', onUploadSuccess: function (file, data, response) { var parentdiv = $(this.wrapper).parent().parent().parent(); var picList = parentdiv.find('.pic-list'); if (data == "0") { $.messager.alert("提示", "上传失败!", 'error'); } else { $.messager.alert("提示", "上传成功!", 'info'); picList.append('<img src="' + data + '"/>'); var scenery = parentdiv.children("input[name='Scenery']"); scenery.val(scenery.val() + '|' + data); } } }); });
后台图片处理代码
using Common; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; namespace Web { /// <summary> /// UploadFiles 的摘要说明 /// </summary> public class UploadFiles : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; HttpPostedFile file = context.Request.Files["Filedata"]; string Dir = "Upload"; string uploadPath = "\" + Dir + "\TravelProduct\" + DateTime.Now.ToString("yyyyMMdd") + "\"; var path=context.Request.QueryString["path"]; if(path!=null && path.ToString()!="" ) uploadPath = "\" + Dir + "\"+path+"\" + DateTime.Now.ToString("yyyyMMdd") + "\"; if (!string.IsNullOrEmpty(context.Request["folder"])) { uploadPath = HttpContext.Current.Server.MapPath(context.Request["folder"]) + "\"; //从页面传过来的路径 } string returnPath = uploadPath.Replace("\", "/"); if (!string.IsNullOrEmpty(context.Request["folder"])) { returnPath = context.Request["folder"].Replace("\", "/")+"/"; } try { uploadPath = HttpContext.Current.Server.MapPath(uploadPath); } catch (Exception e) { } if (file != null) { if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } string fileType = Tool.MakeRandomNumber(8, 1) + Tool.GetPicType(file.FileName); file.SaveAs(uploadPath + fileType); //下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失 context.Response.Write(returnPath + fileType); } else { context.Response.Write("0"); } } public bool IsReusable { get { return false; } } } }