简单是iframe上传封装
还在学习当中希望多提意见,谢谢了
代码
1 (function($) {
2 $.fn.extend({
3 upload: function(options) {
4 var defaults = {
5 //默认参数
6 url: "/anran/", //1,url是上传地址
7 type: "s", //Type操作类型,s是上传,d是删除
8 ImageName: "mag", //imageName上传图片Id
9 up: "file1", //up上传file的name值
10 defultImage: "/Image/fbz.jpg", //defultImage图片显示的默认图片
11 height: "200", //缩略图高度
12 "200", //缩略图宽度
13 mode: "HW", // 缩略图类型
14 WaterPic: "Y" //是否添加水印 y是添加,n是不添加
15 }
16
17 var options = $.extend(defaults, options);
18
19 return this.each(function() {
20
21 uploadImage(options.url, options.type, options.ImageName, options.up, options.defultImage, options.height, options.width, options.mode, options.WaterPic);
22 });
23
//图片上传调用
代码
1 function uploadImage(url, funType, imageName, up, defultImage, height, width, mode, isWaterPic) {
2 $("[Id$='" + up + "']").attr("name", up);
3 var uploadValue = $("[Id$='" + up + "']").val(); //当前上传值
4 var img = $("[id$=" + imageName + "]").get(0).src.substring($("[id$=" + imageName + "]").get(0).src.lastIndexOf('/') + 1); //图片名
5 //点击上传按钮时(先验证了图片格式)
6 if (funType.toString().toUpperCase() == 'S') {
7 var num = 0;
8 if (checkData(uploadValue)) {
9 $("[t='t']").each(function() {
10 if ($(this).val() == uploadValue) {
11 alert("提示:请不要上传相同图片");
12 num++;
13 }
14 })
15 if (num == 0) {
16 if ($("[tag$='" + up + "']").val() == null) {
17 $("#form1").append("<input tag='" + up + "' style='display:none' t='t' type='text'/>");
18 }
19 $("[tag='" + up + "']").val(uploadValue);
20 $("#form1").attr("action", "/Upload/upload.ashx?UploadFile=" + url + "&ImageName=" + imageName + "&up=" + up + "&adddel=" + url + "SmllPic/" + img + "&def=" + defultImage + "&h=" + height + "&w=" + width + "&mode=" + mode + "&isNWaterPic=" + isWaterPic);
21 $("#form1").attr("target", "hd");
22 $("#form1").submit();
23 }
24 }
25 } else if (funType.toString().toUpperCase() == 'D') {
26 if (img == defultImage.substring(defultImage.lastIndexOf('/') + 1)) {
27 return;
28 }
29 // array = array.slice(0, i).concat(array.slice(i + 1)); //对i为最后一个元素或第一个元素都没问题,而且i=-1为最后一个元素.以此类推.
30 $("[tag='" + up + "']").remove();
31 $("#form1").attr("action", "/Upload/upload.ashx?UploadFile=" + url + "&del=" + url + "SmllPic/" + img + "&ImageName=" + imageName + "&up=" + up + "&def=" + defultImage + "&isNWaterPic=" + isWaterPic);
32 $("#form1").attr("target", "hd");
33 $("#form1").submit();
34
35 }
36 }
///验证上传的图片
代码
1 ///验证上传的图片
2 function checkData(upload) {
3 var result = true;
4 var fileName = upload; //获得上传控件对象值
5 if (fileName == "") {
6 alert("提示:请先选择你上传的图片");
7 return false;
8 }
9 var exName = fileName.substr(fileName.lastIndexOf(".") + 1).toUpperCase();
10 if (exName != "JPG" && exName != "BMP" && exName != "GIF" && exName != "JPEG" && exName != "PNG") {
11 alert("提示:只能上传jpg,bmp,gif,jpeg,png格式的图片");
12 result = false;
13 }
14 return result;
15
16 }
//url重新定向
代码
1 function rea() {
2 $("#form1").attr("action", location.href);
3 $("#form1").removeAttr("target");
4 if ($("#form1").attr("target") == null || $("#form1").attr("target") == "") {
5 return true;
6 }
7 else {
8 return false;
9 }
10 }
一般处理程序中的代码
代码
1 public void ProcessRequest(HttpContext context)
2 {
3 HttpRequest Request = context.Request;
4 HttpResponse Response = context.Response;
5 HttpServerUtility Server = context.Server;
6 //指定输出头和编码
7 Response.ContentType = "text/html";
8 Response.Charset = "gb2313";
9 HttpPostedFile f = Request.Files[Request.QueryString["up"]];
10 string sql = Request.QueryString["UploadFile"];
11 //获取上传的文件
12 string oldfile = f.FileName;
13 if (Request.QueryString["def"] != f.FileName)
14 {
15
16 if (File.Exists(Server.MapPath(Request.QueryString["del"])))
17 {
18 File.Delete(Server.MapPath(Request.QueryString["del"]));
19 Response.Write("<script>parent.UpdateMsddg('','','" + Request.QueryString["ImageName"] + "','" + Request.QueryString["def"] + "','" + Request.QueryString["up"] + "')</script>");
20 return;
21 }
22 }
23 if (Request.QueryString["adddel"] != sql + Request.QueryString["def"])
24 {
25 if (File.Exists(Server.MapPath(Request.QueryString["adddel"])))
26 {
27 File.Delete(Server.MapPath(Request.QueryString["adddel"]));
28 }
29 }
30 string nowTime = DateTime.Now.ToString("yyyyMMddHHmmss"); //文件名
31 string oldformate = oldfile.Substring(oldfile.LastIndexOf('.')); //文件格式
32 string newFileName = nowTime + oldfile.Substring(oldfile.LastIndexOf('.')); //文件全名
33
34 if (!Directory.Exists(Server.MapPath(sql + "/SmllPic")))
35 {
36 Directory.CreateDirectory(Server.MapPath(sql + "/SmllPic"));
37 }
38 f.SaveAs(Server.MapPath(sql + newFileName));
39
40
41 //生成缩略图
42 string newFile = sql + "SmllPic/";//缩略图保存途径
43 MakeSmallPic.MakeThumbnail(Server.MapPath(sql + newFileName), Server.MapPath(newFile + newFileName), Convert.ToInt32(Request.QueryString["w"]), Convert.ToInt32(Request.QueryString["h"]), Request.QueryString["mode"]);
44 if (File.Exists(Server.MapPath(sql + newFileName)))
45 {
46 File.Delete(Server.MapPath(sql + newFileName));
47 }
48 if (Request.QueryString["isNWaterPic"].ToUpper() == "Y")
49 {
50 //带上水印/Image/zipsd.png水印图片
51 MakeWaterPic.AddWaterPic(Server.MapPath(newFile + newFileName), Server.MapPath(newFile + nowTime + "1" + oldformate), Server.MapPath("/Image/zipsd.png"));
52
53 if (File.Exists(Server.MapPath(newFile + newFileName)))
54 {
55 File.Delete(Server.MapPath(newFile + newFileName));
56 }
57 //如果要保存到其他地方,注意修改这里
58 //调用父过程更新内容,注意要对des变量进行js转义替换,繁殖字符串不闭合提示错误
59 context.Response.Write("<script>window.parent.UpdateMsddg('" + nowTime + "1" + oldformate + "','" + newFile + "','" + Request.QueryString["ImageName"] + "','" + Request.QueryString["def"] + "','" + Request.QueryString["up"] + "')</script>");
60 }
61 else
62 {
63 //如果要保存到其他地方,注意修改这里
64 //调用父过程更新内容,注意要对des变量进行js转义替换,繁殖字符串不闭合提示错误
65 context.Response.Write("<script>window.parent.UpdateMsddg('" + nowTime + oldformate + "','" + newFile + "','" + Request.QueryString["ImageName"] + "','" + Request.QueryString["def"] + "','" + Request.QueryString["up"] + "')</script>");
66 }
67
68
69
70 }