1 <div>
2 <form id="uploadForm" action="Upload" method="post" enctype="multipart/form-data">
3 <table>
4 <tbody>
5 <tr>
6 <td class="td03" style="padding-top: 18px; vertical-align: top"><span class="xxx">*</span>产品图片 </td>
7 <td class="td02" style="padding-top: 9px">
8 <div class="upload">
9 <input type="button" id="btnbra" class="btn" onclick="browerfilebrand.click()" value="上传">
10 <input type="file" id="browerfilebrand" class="upload-input-filebra" name="fileupload" accept="image/gif, image/jpeg" multiple="multiple" value="">
11 <div class="img_center" id="braimage">
12 <img src="~/Images/@ViewBag.image" class="bra-img">
13 </div>
14 <input data-val="true" name="imgname" type="hidden" value="@ViewBag.image" />
15 </div>
16 <div class="tip">建议图片尺寸为600*600像素以上正方形,图片请避免全文字</div>
17 </td>
18 </tr>
19 </tbody>
20 </table>
21 </form>
22 <div id="SaveBrand" class="fabu">
23 <a>保存</a>
24 </div>
25 </div>
26
27 <script type="text/javascript">
28 //点击保存上传品牌信息
29 $("#SaveBrand").click(function () {
30
31 var formData = new FormData($("#uploadForm")[0]);
32 $.ajax({
33 type: "POST",
34 data: formData,
35 url: "/Upload/UploadImage",
36 contentType: false,
37 processData: false,
38 }).success(function (data) {
39
40 if (data.status) {
41 window.location.href = '@Url.Action("Brand", "Upload")?bid=&b=';
42 //alert(data.url);
43 }
44 else {
45 alert(data.msg);
46 }
47 }).error(function (data) {
48 alert(data.msg);
49 });
50 });
51 </script>
52
53
54 [HttpPost]
55 public JsonResult UploadImage(FormCollection collection)
56 {
57 if (Request.Files.Count > 0)
58 {
59 if (Request.Files.Count == 1)
60 {
61 HttpPostedFileBase file = Request.Files[0];
62 if (file.ContentLength > 0)
63 {
64 string title = string.Empty;
65
66 title = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Path.GetFileName(file.FileName);
67 string path = "../Images/" + title;
68 string sendimgstr = "/Images/" + title;
69 path = System.Web.HttpContext.Current.Server.MapPath(path);
70 file.SaveAs(path);
71
72 string bId = collection["brandid"];//品牌ID
73 string bName = collection["BrandName"];//品牌名称
74 string bInfo = collection["BrandInfo"];//品牌网址
75 string bImage = title;//品牌图片
76 string CatId = collection["CatalogID"];//所属类别
77 DataTable dt = null;
78 string astr = DAL.BrandInfo.GetBrandName(bName);
79
80 if (!string.IsNullOrEmpty(bId) && !string.IsNullOrEmpty(bName) && !string.IsNullOrEmpty(bImage) && !string.IsNullOrEmpty(CatId))
81 {
82 if (astr != "0") //存在此品牌,给出提示
83 {
84 //执行修改品牌
85 dt = DAL.BrandInfo.UpdateBrand(bId, bName, bInfo, bImage, CatId);
86 }
87 else //执行上传品牌
88 {
89 dt = DAL.BrandInfo.AddBrand(bId, bName, bInfo, bImage, CatId);
90 }
91 }
92 else
93 {
94 //执行上传品牌
95 dt = DAL.BrandInfo.AddBrand(bId, bName, bInfo, bImage, CatId);
96 }
97
98 if (astr != "0")
99 {
100 return Json(new { status = true, url = sendimgstr, msg = "" });
101 }
102 else
103 {
104 return Json(new { status = true, url = sendimgstr, msg = "" });
105 }
106 }
107 else
108 {//没有换图片的时候
109 string bId = collection["brandid"];//品牌ID
110 string bName = collection["BrandName"];//品牌名称
111 string bInfo = collection["BrandInfo"];//品牌网址
112 string bImage = collection["imgname"];//品牌图片
113 string CatId = collection["CatalogID"];//所属类别
114 DataTable dt = null;
115 string astr = DAL.BrandInfo.GetBrandName(bName);
116
117 if (!string.IsNullOrEmpty(bId) && !string.IsNullOrEmpty(bName) && !string.IsNullOrEmpty(bImage) && !string.IsNullOrEmpty(CatId))
118 {
119 if (astr != "0") //存在此品牌,给出提示,不执行操作
120 {
121 //执行修改品牌
122 dt = DAL.BrandInfo.UpdateBrand(bId, bName, bInfo, bImage, CatId);
123 }
124 else //执行上传品牌
125 {
126 dt = DAL.BrandInfo.AddBrand(bId, bName, bInfo, bImage, CatId);
127 }
128 }
129 else
130 {
131 //执行修改品牌
132 dt = DAL.BrandInfo.UpdateBrand(bId, bName, bInfo, bImage, CatId);
133 }
134 if (astr != "0")
135 {
136 string sendimgstr = "/Images/" + bImage;
137 return Json(new { status = true, url = sendimgstr, msg = "" });
138 }
139 else
140 {
141 string sendimgstr = "/Images/" + bImage;
142 return Json(new { status = true, url = sendimgstr, msg = "" });
143 }
144
145 }
146 }
147 else
148 {
149 string[] urllist = new string[Request.Files.Count];
150 for (int i = 0; i < Request.Files.Count; i++)
151 {
152 HttpPostedFileBase file = Request.Files[i];
153 if (file.ContentLength > 0)
154 {
155 string title = string.Empty;
156 title = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Path.GetFileName(file.FileName);
157 string path = "../Images/" + title;
158 path = System.Web.HttpContext.Current.Server.MapPath(path);
159 file.SaveAs(path);
160 urllist[i] = path;
161 }
162 }
163 return Json(new { status = true, url = urllist });
164 }
165
166 }
167 else
168 {
169 return Json(new { status = false, url = "", msg = "没有文件" });
170 }
171 //return Json(new { status = false, url = "", msg = "" });
172 }