btnupload:function(){
var that=this;
var cocahId=getApp().globalData.cocahId;
//选取图片上传
wx.chooseImage({
count: 1, // 默认9
sizeType: [‘original’, ‘compressed’], // 可以指定是原图还是压缩图,默认二者都有
sourceType: [‘album’, ‘camera’], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
var tempFilePaths = res.tempFilePaths
that.setData({
tupath: tempFilePaths[0]
})
//上传文件
wx.uploadFile({
url: url + ‘/api/DataApi/AddNEWCocahClass/’,
filePath: tempFilePaths[0],
name: ‘photo’,
header: {
‘content-type’: ‘multipart/form-data’
}, // 设置请求的 header
formData: {
‘Id’: cocahId,
‘name’: tempFilePaths[0]
}, // HTTP 请求中其他额外的 form data
success: function (result) {
console.log(‘123’)
console.log(result)
that.setData({
Id: result.data
})
}
//结束标识符
})
}
//选取图片上传结束
})
},
这个后台的接口该如何写?
现在后台的接口调试的是报错404
[HttpPost]
public HttpResponseMessage AddNEWCocahClass(HttpPostedFileBase file, int Id, string name)
{
try
{
#region 保存图片
string virtualPath = “/UploadFile/CocahClassImg/”;
string path = HttpContext.Current.Server.MapPath(virtualPath);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//先将文件保存到服务器
string fileName =Guid.NewGuid().ToString() + System.IO.Path.GetExtension(file.FileName);
file.SaveAs(path + fileName);
//截图
HelpTool.MakeThumbnail(path + fileName, path + “thumb_” + fileName, 600, 600);
//删除原文件
HelpTool.deleteFile(path + fileName);
#endregion
return ReturnHttpResponse(JsonConvert.SerializeObject(-1));
}
catch (Exception ex)
{
return ReturnHttpResponse(JsonConvert.SerializeObject(-2));
}
}