testupload(){
var that = this;
//选取图片上传
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/UploadImg’,
filePath: tempFilePaths[0],
name: ‘file’,
header: { ‘Content-Type’:‘multipart/form-data’},
success:function(jieguo){
console.log(‘上传结果’)
console.log(jieguo)
}
})
}
//选取图片上传结束
})
},
后台方法如下:
#region 测试上传图片
[HttpPost]
public string UploadImg(HttpPostedFileWrapper file)
{
try
{
return Guid.NewGuid().ToString();
}
catch (Exception ex)
{
return “ABC”;
}
}
#endregion
报错如下:
后台我什么都没有做啊!
#region 上传图片
[HttpPost]
public string UploadCocahClassImg() {
string virtualPath = "*****";//图片存放在服务器的文件夹
string path = HttpContext.Current.Server.MapPath(virtualPath);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
try {
Stream stream = System.Web.HttpContext.Current.Request.Files[0].InputStream;//读取上传文件的文件流
//生成图片
Bitmap bmp = new Bitmap(stream);
Bitmap newbmp = new Bitmap(bmp, 300, 300);
string fileName = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(System.Web.HttpContext.Current.Request.Files[0].FileName);
newbmp.Save(path + fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
//截图
HelpTool.MakeThumbnail(path + fileName, path + "thumb_" + fileName, 300, 300);
//删除原文件
HelpTool.deleteFile(path + fileName);
return virtualPath + "thumb_" + fileName;
}
catch (Exception ex) {
return "ABC";
}
}
#endregion