c#后端 小程序上传图片
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#后端 小程序上传图片的更多相关文章
- 微信小程序上传图片(附后端代码)
几乎每个程序都需要用到图片. 在小程序中我们可以通过image组件显示图片. 当然小程序也是可以上传图片的,微信小程序文档也写的很清楚. 上传图片 首先选择图片 通过wx.chooseImage(OB ...
- .NET开发微信小程序-上传图片到服务器
1.上传图片分为几种: a:上传图片到本地(永久保存) b:上传图片到本地(临时保存) c:上传图片到服务器 a和b在小程序的api文档里面有.直接说C:上传图片到服务器 前端代码: /* 上传图片到 ...
- 小程序上传图片功能 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 ...
- 微信小程序上传图片及本地测试
前端(.wxml) <view id="view1"> <view id="btns"> <image id="ima1 ...
- (十)微信小程序---上传图片chooseImage
官方文档 示例一 wxml <view bindtap="uploadImage">请上传图片</view> <image wx:for=" ...
- 微信小程序 上传图片并等比列压缩到指定大小
微信小程序官方API中 wx.chooseImage() 是可以进行图片压缩的,可惜的是不能压缩到指定大小. 实际开发中需求可能是压缩到指定大小: 原生js可以使用canvas来压缩,但由于微信小程 ...
- 微信小程序上传图片(前端+PHP后端)
一.wxml文件 <text>上传图片</text> <view> <button bindtap="uploadimg">点击选择 ...
- 微信小程序 - 上传图片(组件)
更新日期: 2019/3/14:首次发布,更新了2018/12/30的UI以及反馈信息获取方式,具体请下载:demo. 2019/3/20:感谢544429676@qq.com指出的现存bug,已修复 ...
- 微信小程序上传图片,视频及预览
wxml <!-- 图片预览 --> <view class='preview-warp' wx:if="{{urls}}"> <image src= ...
随机推荐
- 献给即将35岁的初学者,焦虑 or 出路?
导言:“对抗职场“35 岁焦虑”,也许唯一的方法是比这个瞬息万变的商业社会跑得更快!” 一直以来,都有许多人说“程序员或测试员是个吃青春饭的职业”,甚至还有说“35 岁混不到管理就等于失业”的言论. ...
- java8 常用代码
1. 使用java8 提取出 list 中 bean 的某一属性 public static void main(String[] args) { List<Student> stuLis ...
- C++ Primer抄书笔记(二)——变量和基本类型(下)
四.const限定符[引用/指针/顶层/常量表达式] const对象值不变,必须初始化,能完成此type的大部分operation. 一般,多文件独立变量,编译初始化仅文件内有效: 除非,(条件:初值 ...
- CDC+ETL实现数据集成方案
欢迎咨询,合作! weix:wonter 名词解释: CDC又称变更数据捕获(Change Data Capture),开启cdc的源表在插入INSERT.更新UPDATE和删除DELETE活动时会插 ...
- sql对于表格中列的删改
mysql与oracle char为定长字符串 var为可变字符串 修改表名:rename table1 to table2:(mysql) alter table1 rename to table2 ...
- alpine安装telnet等工具
alpine确实是很精简,但是对于熟悉了centos和ununtu的个人来说,实在是不习惯. 因此,记录关于alpine的一些包安装,以及操作细节(逐渐补充). 1. telnet >>& ...
- clr via c# 定制特性
1,特性的应用范围:特性可应用于程序集,模块,类型,字段,方法,方法参数,方法返回值,属性,参数,泛型参数 2,利用前缀告诉编译器表明意图---下面的倾斜是必须的表明了我们的目标元素: [assemb ...
- git 流程
1.git clone 拉取代码2.git checkout -b '分支名称' 命令意思: 创建并切换到当前新建的本地分支. 查看并创建分支,先远端,后本地.3.将本地分支和远端分之关联 git b ...
- 洛谷题解 P1292 【倒酒】
原题传送门 题目描述 Winy是一家酒吧的老板,他的酒吧提供两种体积的啤酒,a ml和b ml,分别使用容积为a ml和b ml的酒杯来装载. 酒吧的生意并不好.Winy发现酒鬼们都非常穷.有时,他们 ...
- 关于Synchronized研伸扩展
代码1 synchronized方法 synchronized void method(){ .......... } 代码2 synchronized代码块 synchronized (obj){ ...