c#后端:

        /// <summary>
/// 上传图片
/// </summary>
/// <returns></returns>
[HttpPost]
public ResultData uploadImage()
{
ResultData result = new ResultData();
try
{
string path = "/tmp/";
HttpPostedFile file = System.Web.HttpContext.Current.Request.Files["content"]; //对应小程序 name
string parameters = string.Format("postData:{0}", file.ToString()); //获取文件
if (file != null)
{
Stream sr = file.InputStream; //文件流
Bitmap bitmap = (Bitmap)Bitmap.FromStream(sr);
path += file.FileName;
string currentpath = System.Web.HttpContext.Current.Server.MapPath("~"); bitmap.Save(currentpath + path);
}
result.status = ;
result.data = path;
}
catch (Exception vErr)
{
result.status = -;
result.detail = vErr.Message;
return result;
}
return result;
}
    public class ResultData
{ public int status { get; set; } public string data { get; set; } public string detail { get; set; } }

小程序前端:

upFiles.js

var chooseImage = (t, count) =>{
wx.chooseImage({
count: count,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: (res) => {
var imgArr = t.data.upImgArr || [];
let arr = res.tempFiles;
// console.log(res)
arr.map(function(v,i){
v['progress'] = ;
imgArr.push(v)
})
t.setData({
upImgArr: imgArr
}) let upFilesArr = getPathArr(t);
if (upFilesArr.length > count-) {
let imgArr = t.data.upImgArr;
let newimgArr = imgArr.slice(, count)
t.setData({
upFilesBtn: false,
upImgArr: newimgArr
})
}
},
});
}
var chooseVideo = (t,count) => {
wx.chooseVideo({
sourceType: ['album', 'camera'],
maxDuration: ,
compressed:true,
camera: 'back',
success: function (res) {
let videoArr = t.data.upVideoArr || [];
let videoInfo = {};
videoInfo['tempFilePath'] = res.tempFilePath;
videoInfo['size'] = res.size;
videoInfo['height'] = res.height;
videoInfo['width'] = res.width;
videoInfo['thumbTempFilePath'] = res.thumbTempFilePath;
videoInfo['progress'] = ;
videoArr.push(videoInfo)
t.setData({
upVideoArr: videoArr
})
let upFilesArr = getPathArr(t);
if (upFilesArr.length > count - ) {
t.setData({
upFilesBtn: false,
})
}
// console.log(res)
}
})
} // 获取 图片数组 和 视频数组 以及合并数组
var getPathArr = t => {
let imgarr = t.data.upImgArr || [];
let upVideoArr = t.data.upVideoArr || [];
let imgPathArr = [];
let videoPathArr = [];
imgarr.map(function (v, i) {
imgPathArr.push(v.path)
})
upVideoArr.map(function (v, i) {
videoPathArr.push(v.tempFilePath)
})
let filesPathsArr = imgPathArr.concat(videoPathArr);
return filesPathsArr;
} /**
* upFilesFun(this,object)
* object:{
* url ************ 上传路径 (必传)
* filesPathsArr ****** 文件路径数组
* name ****** wx.uploadFile name
* formData ****** 其他上传的参数
* startIndex ****** 开始上传位置 0
* successNumber ****** 成功个数
* failNumber ****** 失败个数
* completeNumber ****** 完成个数
* }
* progress:上传进度
* success:上传完成之后
*/ var upFilesFun = (t, data, progress, success) =>{
let _this = t;
let url = data.url;
let filesPath = data.filesPathsArr ? data.filesPathsArr : getPathArr(t);
let name = data.name || 'file';
let formData = data.formData || {};
let startIndex = data.startIndex ? data.startIndex : ;
let successNumber = data.successNumber ? data.successNumber : ;
let failNumber = data.failNumber ? data.failNumber : ;
if (filesPath.length == ) {
success([]);
return;
}
const uploadTask = wx.uploadFile({
url: url,
filePath: filesPath[startIndex],
name: name,
formData: formData,
success: function (res) {
var data = res.data
successNumber++;
// console.log('success', successNumber)
// console.log('success',res)
// 把后台返回的地址链接存到一个数组
let uploaded = t.data.uploadedPathArr || [];
var da = JSON.parse(res.data);
// console.log(da)
if (da.code == ) {
// ### 此处可能需要修改 以获取图片路径
uploaded.push(da.data) t.setData({
uploadedPathArr: uploaded
})
}
},
fail: function(res){
failNumber++;
// console.log('fail', filesPath[startIndex])
// console.log('failstartIndex',startIndex)
// console.log('fail', failNumber)
// console.log('fail', res)
},
complete: function(res){ if (startIndex == filesPath.length - ){
// console.log('completeNumber', startIndex)
// console.log('over',res)
let sucPathArr = t.data.uploadedPathArr;
success(sucPathArr);
t.setData({
uploadedPathArr: []
})
console.log('成功:' + successNumber + " 失败:" + failNumber)
}else{
startIndex++;
// console.log(startIndex)
data.startIndex = startIndex;
data.successNumber = successNumber;
data.failNumber = failNumber;
upFilesFun(t, data, progress, success);
}
}
}) uploadTask.onProgressUpdate((res) => {
res['index'] = startIndex;
// console.log(typeof (progress));
if (typeof (progress) == 'function') {
progress(res);
}
// console.log('上传进度', res.progress)
// console.log('已经上传的数据长度', res.totalBytesSent)
// console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
}) }
module.exports = { chooseImage, chooseVideo, upFilesFun, getPathArr}

参考 https://www.cnblogs.com/flysem/p/9346759.html

c#后端 小程序上传图片的更多相关文章

  1. 微信小程序上传图片(附后端代码)

    几乎每个程序都需要用到图片. 在小程序中我们可以通过image组件显示图片. 当然小程序也是可以上传图片的,微信小程序文档也写的很清楚. 上传图片 首先选择图片 通过wx.chooseImage(OB ...

  2. .NET开发微信小程序-上传图片到服务器

    1.上传图片分为几种: a:上传图片到本地(永久保存) b:上传图片到本地(临时保存) c:上传图片到服务器 a和b在小程序的api文档里面有.直接说C:上传图片到服务器 前端代码: /* 上传图片到 ...

  3. 小程序上传图片功能 uploadFile:fail Read error:ssl=0xa738d808:I/O error during system call,Connection reset by peer

    由于纯网页上传图片小程序会闪退,就采用了小程序原生的上传功能wx.uploadfile 处理流程: 1.网页需要跳转到小程序 需要引用 <script src='https://res.wx.q ...

  4. 微信小程序上传图片及本地测试

    前端(.wxml) <view id="view1"> <view id="btns"> <image id="ima1 ...

  5. (十)微信小程序---上传图片chooseImage

    官方文档 示例一 wxml <view bindtap="uploadImage">请上传图片</view> <image wx:for=" ...

  6. 微信小程序 上传图片并等比列压缩到指定大小

    微信小程序官方API中  wx.chooseImage() 是可以进行图片压缩的,可惜的是不能压缩到指定大小. 实际开发中需求可能是压缩到指定大小: 原生js可以使用canvas来压缩,但由于微信小程 ...

  7. 微信小程序上传图片(前端+PHP后端)

    一.wxml文件 <text>上传图片</text> <view> <button bindtap="uploadimg">点击选择 ...

  8. 微信小程序 - 上传图片(组件)

    更新日期: 2019/3/14:首次发布,更新了2018/12/30的UI以及反馈信息获取方式,具体请下载:demo. 2019/3/20:感谢544429676@qq.com指出的现存bug,已修复 ...

  9. 微信小程序上传图片,视频及预览

    wxml <!-- 图片预览 --> <view class='preview-warp' wx:if="{{urls}}"> <image src= ...

随机推荐

  1. Hadoop搭建record下

    前言 先说一下当前环境:Ubuntu18.04 jdk1.8 Hadoop选用-2.6.0-cdh5.15.1 用户名:supershuai-VirtualBox Hadoop的下载地址:http:/ ...

  2. pikachu-暴力破解漏洞解析

    本篇blog导航 ~暴力破解&暴力破解漏洞概述 ~基于表单的暴力破解实验 ~暴力破解的绕过和防范(验证码&Token)     ~验证码的基础知识     ~验证码绕过(on clie ...

  3. 通配符与标签!important的背景展示,也是让我怀疑人生了

    是谁在耳边对我说!important提升权重优先级,只为这一句,我用了3600s研究通配符与标签!important的背景展示,也是让我怀疑人生了!选择器权值:标签选择器1,类选择器和伪类选择器:10 ...

  4. 微信小程序如何下载超过大小限制(10M)的视频?(苹果用户仔细看,安卓用户快速看)

    众所周知,微信小程序对下载的文件大小有限制,目前是最大支持10M.我们在用去水印小程序保存视频的时候,如果遇到长视频,视频大小可能就超过限制.遇到这种情况,我们如何才能把视频保存到手机相册呢? 首先, ...

  5. .net core 3.1 webapi后端接收钉钉小程序post的文件/图片

    世上本没路:走的人多了,便成了路. dd.uploadFile({ url: '请使用自己服务器地址', fileType: 'image', fileName: 'file', filePath: ...

  6. Python 中使用 Pillow 处理图片增加水印

    这个是个比较常见的需求,比如你在某个网站上发布了图片,在图片上就会出现带你昵称的水印.那么在Python中应该如何处理这一类需求呢? 其实在我的<Django实战开发>视频教程中有讲到这一 ...

  7. pip 自己的源 搭建

    1  安装工具 pip install  pip2pi 2  下载 所需要的包 pip2tgz /application/nginx/html/yum/python/ apscheduler (172 ...

  8. Luarocks 安装艰难过程

    https://www.cnblogs.com/fanxiaojuan/p/11551268.html

  9. Redis的启动和关闭(前台启动和后台启动)

    场景 Centos中Redis的下载编译与安装(超详细): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103967334 在上 ...

  10. 版本管理git

    Git 是目前世界上最先进的分布式版本控制系统. git的主要操作步骤 git.init  初始化,显示成功后去相应的文件夹中查看是不是多了一个git文(版本库)  命令1.    git confi ...