微信小程序:
uploadOneByOne(imgPaths, successUp, failUp, count, length, data) {
if (length == 0) {
wx.showToast({
title: '上传图片少于一张',
icon: 'none',
duration: 2000
}) return false;
}
console.log("调用测试") var that = this;
wx.showLoading({
title: '正在上传第' + (count + 1) + '张',
}) wx.uploadFile({
url: 'http://wangzhengjie.free.idcfengye.com/api/values/PostFile',
//接口地址
filePath: imgPaths[count],
name: wx.getStorageSync("openid"),
//使用顺序给文件命名
formData: data,
success: function(e) {
successUp++; //成功+1
},
fail: function(e) {
failUp++; //失败+1
},
complete: function(e) {
debugger count++; //下一张
if (count == length) {
wx.hideLoading()
//上传完毕,作一下提示
console.log('上传成功' + successUp + ',' + '失败' + failUp);
return true wx.showToast({
title: '上传成功' + successUp,
icon: 'success',
duration: 2000
})
} else {
//递归调用,上传-------------------------------------------------------------------------下一张
that.uploadOneByOne(imgPaths, successUp, failUp, count, length);
console.log('正在上传第' + count + '张');
}
}
})
}
Service:
[HttpPost]
[Route("PostFile")]
public string PostFile([FromForm] IFormCollection formCollection)
{
saveTest saveTest = new saveTest();
string str1 = "Fail";
//获取jsonData数据
decimal jianshu = Convert.ToDecimal(formCollection["jianshu"].ToString());
string str2 = "a";
//str2 = Convert.ToString(formCollection.get_Item("name"));
foreach( var file in formCollection.Files)
{
string path = "Test/" + file.FileName;
IFormFile current = file;
using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
{
current.CopyTo((Stream)fileStream);
((Stream)fileStream).Flush();
}
}
return str1;
}