前端代码
- data: {
- productInfo: {}
- },
- bindChooiceProduct: function () {
- var that = this;
-
- wx.chooseImage({
- count: 3,
- sizeType: ['compressed'],
- sourceType: ['album', 'camera'],
- success: function (res) {
-
- var tempFilePaths = res.tempFilePaths;
-
- wx.showToast({
- title: '正在上传...',
- icon: 'loading',
- mask: true,
- duration: 10000
- })
- var uploadImgCount = 0;
- for (var i = 0, h = tempFilePaths.length; i < h; i++) {
- wx.uploadFile({
- url: util.getClientSetting().domainName + '/home/uploadfilenew',
- filePath: tempFilePaths[i],
- name: 'uploadfile_ant',
- formData: {
- 'imgIndex': i
- },
- header: {
- "Content-Type": "multipart/form-data"
- },
- success: function (res) {
- uploadImgCount++;
- var data = JSON.parse(res.data);
-
- var productInfo = that.data.productInfo;
- if (productInfo.bannerInfo == null) {
- productInfo.bannerInfo = [];
- }
- productInfo.bannerInfo.push({
- "catalog": data.Catalog,
- "fileName": data.FileName,
- "url": data.Url
- });
- that.setData({
- productInfo: productInfo
- });
-
-
- if (uploadImgCount == tempFilePaths.length) {
- wx.hideToast();
- }
- },
- fail: function (res) {
- wx.hideToast();
- wx.showModal({
- title: '错误提示',
- content: '上传图片失败',
- showCancel: false,
- success: function (res) { }
- })
- }
- });
- }
- }
- });
- }
后端上传代码(将文件上传到服务器临时文件夹内)
- [HttpPost]
- public ContentResult UploadFileNew()
- {
- UploadFileDTO model = new UploadFileDTO();
- HttpPostedFileBase file = Request.Files["uploadfile_ant"];
- if (file != null)
- {
-
- model.Catalog = DateTime.Now.ToString("yyyyMMdd");
- model.ImgIndex = Convert.ToInt32(Request.Form["imgIndex"]);
-
-
- string extensionName = System.IO.Path.GetExtension(file.FileName);
-
-
- model.FileName = System.Guid.NewGuid().ToString("N") + extensionName;
-
-
- string filePathName = System.IO.Path.Combine(CommonHelper.GetConfigValue("ImageAbsoluteFolderTemp"), model.Catalog);
- if (!System.IO.Directory.Exists(filePathName))
- {
- System.IO.Directory.CreateDirectory(filePathName);
- }
-
- string relativeUrl = CommonHelper.GetConfigValue("ImageRelativeFolderTemp");
- file.SaveAs(System.IO.Path.Combine(filePathName, model.FileName));
-
-
- model.Url = System.IO.Path.Combine(relativeUrl, model.Catalog, model.FileName).Replace("\\", "/");
- }
- return Content(Newtonsoft.Json.JsonConvert.SerializeObject(model));
- }
- public class UploadFileDTO
- {
-
-
-
- public string Catalog { set; get; }
-
-
-
- public string FileName { set; get; }
-
-
-
- public string Url { set; get; }
-
-
-
- public int ImgIndex { get; set; }
- }
- #region 获取配置文件Key对应Value值
- public static string GetConfigValue(string key)
- {
- return ConfigurationManager.AppSettings[key].ToString();
- }
- #endregion
设置配置文件上传文件对应的文件夹信息
- <appSettings>
-
- <add key="ImageAbsoluteFolderTemp" value="D:\Images\temp" />
-
- <add key="ImageAbsoluteFolderFinal" value="D:\Images\product" />
-
-
- <add key="ImageRelativeFolderTemp" value="http://192.168.1.79:9009/temp"/>
-
- <add key="ImageRelativeFolderFinal" value="http://192.168.1.79:9009/product"/>
- </appSettings>
PS:上传到服务器的临时文件夹
posted @
2018-01-22 19:57
yangchunlong
阅读(
4839)
评论()
收藏
举报