1,前台代码:
1 <head runat="server">
2 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
3 <title></title>
4 <script src="../../jquery-easyui-1.4.2/jquery.min.js"></script>
5 </head>
6 <body>
7 <form id="form1" method="post">
8 <div>
9 <table>
10 <tr>
11 <td style=" 80px; text-align: right">上传图片:</td>
12 <td>
13 <a id="ImgJump" name="ImgJump" href="" target="_blank">
14 <img id="Picture" name="Picture" src="" style=" 150px; height: 150px" />
15 </a>
16 <input type="file" id="ImgFile" name="ImgFile" multiple="multiple" />
17 <%-- multiple="multiple"属性为多图片--%>
18 </td>
19 </tr>
20 </table>
21 <input type="hidden" id="ImgHidden" value="" />
22 </div>
23 </form>
24 <script>
25 $("#ImgFile").change(function () {
26 debugger;
27 var falelist = $("#ImgFile").prop('files');
28 let formData = new FormData();
29 for (let i = 0; i < falelist.length; i++) {
30 formData.append(i, falelist[i]);
31 }
32 $.ajax({
33 url: '../../Ashx/BannerManage.ashx?faction=SaveProdImage',
34 type: "POST",
35 data: formData,
36 cache: false,//不缓存
37 contentType: false,// jQuery不要去设置Content-Type请求头
38 processData: false,// jQuery不要去处理发送的数据
39 success: function (data) {
40 $("#ImgHidden").val(data.ResultObject[0].AccessoryUrl);
41 var url = data.ResultObject[0].AccessoryUrl;
42 var imgPre = document.getElementById('Picture');
43 imgPre.src = url;
44 var imgPre_a = document.getElementById('ImgJump');
45 imgPre_a.href = url;
46 imgPre.style.display = 'block';
47 }
48 })
49 })
50 </script>
51 </body>
2,后台代码:
public static string UploadFile()
{
var filelist = HttpContext.Current.Request.Files;
if (filelist.Count == 0)
return Newtonsoft.Json.JsonConvert.SerializeObject(new { ResultCode = 0, ResultMsg = "上传失败" });
string NewFiles = DateTime.Now.ToString("yyyyMM") + "ymall";
List<AccessoryDTO> accessoryList = new List<AccessoryDTO>();
for (int i = 0; i < filelist.Count; i++)
{
var file = filelist[i];
string suffix = Path.GetExtension(file.FileName);
string fileName = Guid.NewGuid() + suffix.ToLower();
//string url = "";
byte[] buffer = new byte[file.InputStream.Length];
file.InputStream.Read(buffer, 0, (int)file.InputStream.Length);
Utility.Net.FtpArgs args = new Utility.Net.FtpArgs()
{
Host = ConfigurationManager.AppSettings["ProductImg:FtpHost2"],
Port = Convert.ToInt32(ConfigurationManager.AppSettings["ProductImg:FtpPort2"]),
UserName = ConfigurationManager.AppSettings["ProductImg:FtpUserName2"],
Userpwd = ConfigurationManager.AppSettings["ProductImg:FtpUserpwd2"],
RemoteDir = "/" + NewFiles + "/",
RemoteFileName = fileName,
UploadData = buffer
};
Utility.ReturnMessage rm = Utility.Net.CFTP.Upload(args);
if (!rm.Success)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(new { ResultCode = 0, ResultObject = accessoryList, ResultMsg = "上传异常" });
//continue;
}
AccessoryDTO dto = new AccessoryDTO();
dto.AccessoryFileType = file.ContentType;
if (dto.AccessoryFileType!= "image/jpeg"&& dto.AccessoryFileType != "image/png") //jpg
{
return Newtonsoft.Json.JsonConvert.SerializeObject(new { ResultCode = 0, ResultObject = accessoryList, ResultMsg = "请上传jpg,png格式!!" });
}
dto.AccessoryName = file.FileName;
dto.AccessorySize = file.ContentLength;
dto.AccessoryUrl = ConfigurationManager.AppSettings["ProductImg:HttpHost2"] + NewFiles + "/" + fileName;
dto.Sort = i;
accessoryList.Add(dto);
}
return Newtonsoft.Json.JsonConvert.SerializeObject(new { ResultCode = 1, ResultObject = accessoryList, ResultMsg = "上传成功!!" });
}
3,web配置
1 <!--图上传FTP路径-->
2 <add key="ProductImg:HttpHost2" value="ftp://192.168.10.32:99/" />
3 <add key="ProductImg:FtpHost2" value="192.168.10.32" />
4 <add key="ProductImg:FtpPort2" value="99" />
5 <add key="ProductImg:FtpUserName2" value="" />
6 <add key="ProductImg:FtpUserpwd2" value="" />
View Code