官方文档

示例一

  • wxml
<view bindtap="uploadImage">请上传图片</view>

<image wx:for="{{imageList}}" src="{{item}}"></image>
  • js
Page({

  /**
* 页面的初始数据
*/
data: {
imageList:[
"/static/default.png",
"/static/default.png",
"/static/default.png",
]
},
uploadImage:function(){
var that = this;
wx.chooseImage({
count: ,                   // 最多可以选择的图片张数  
sizeType: ['original', 'compressed'], // 选择图片的尺寸
sourceType: ['album', 'camera'], // 选择图片的来源
success: function(res) {
that.setData({
imageList:res.tempFilePaths
})
},
})
},
})

示例二

如上 我们有原来的三张图 我们现在在选两张 是要添加进去 如何做?

先学习一下js的知识

1. 列表里添加  push

v1=[,]
>>> () [, ]
v1.push()
v1
>>>() [, , ]

2. 合并列表 concat

v1=[,2,3]
>>>() [, 2, 3] v2=[,]
>>>() [, ]
v1.concat(v2)
>>>() [, , , , ]

微信中添加照片

Page({
data: {
imageList:[
"/static/default.png",
"/static/default.png",
"/static/default.png",
]
},
uploadImage:function(){
var that = this;
wx.chooseImage({
count: ,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: function(res) {
that.setData({
imageList:that.data.imageList.concat(res.tempFilePaths)
})
},
})
},
})

上传到服务器

我们的例子是 腾讯云的存储  https://console.cloud.tencent.com/cos5

1. 创建存储桶

2. 小程序上传的文档

https://cloud.tencent.com/document/product/436/31953

下载好js代码 我们就可以直接用了

3. 开始使用

4.配置项--- 也就是上传配置

官方文档给了四种方式

我们先使用第四种---不推荐

好了 我们的代码

var COS = require('./../../utils/cos-wx-sdk-v5.js')
Page({
data: {
imageList: []
},
上传文件的代码
uploadImage:function(){
var that = this;
wx.chooseImage({
count: ,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: function (res) {
that.setData({
imageList: that.data.imageList.concat(res.tempFilePaths)
})
},
})
},
发布的代码
uploadImageFile:function(){
var cos = new COS({
SecretId: '******',
SecretKey: '*****',
});
for(var index in this.data.imageList){
   循环得到图片地址
var filePath = this.data.imageList[index]
   自己做文件的名字
var filename = filePath.substr(filePath.lastIndexOf('/')+); cos.postObject({
     // 桶的名字
Bucket: 'upload-******',
Region: 'ap-chengdu',
Key: filename,
FilePath: filePath, onProgress: function (info) {
// 上传可以写进度条
console.log(JSON.stringify(info));
}
}, function (err, data) {
console.log(err || data);
});
} },
})

上面我们说的是官方不推荐使用的一种方法,现在说一种推荐使用的方式

https://cloud.tencent.com/document/product/436/14048

开始咯

官网代码获取临时秘钥
var cos = new COS({
// 必选参数
getAuthorization: function (options, callback) {
// 服务端 JS 和 PHP 示例:https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/
// 服务端其他语言参考 COS STS SDK :https://github.com/tencentyun/qcloud-cos-sts-sdk ① 点击
// STS 详细文档指引看:https://cloud.tencent.com/document/product/436/14048
wx.request({
url: 'https://example.com/server/sts.php',
data: {
// 可从 options 取需要的参数
},
success: function (result) {
var data = result.data;
var credentials = data.credentials;
callback({
TmpSecretId: credentials.tmpSecretId,
TmpSecretKey: credentials.tmpSecretKey,
XCosSecurityToken: credentials.sessionToken,
ExpiredTime: data.expiredTime,
});
}
});
}
}); 注释:
① 点击:点击上面的网址找到python相关 按文档操作【安装 拷贝源码】 
pip install -U qcloud-python-sts
拷贝源码https://github.com/tencentyun/qcloud-cos-sts-sdk/blob/master/python/demo/sts_demo.py
from django.conf.urls import url
from app01 import views
urlpatterns = [
# 获取秘钥
url(r'^credential/', views.CredentialView.as_view()),
] class CredentialView(APIView):
def get(self, request, *agrs, **kwargs):
config = {
# 临时密钥有效时长,单位是秒
'duration_seconds': ,
'secret_id': '***********',
# 固定密钥
'secret_key': '***********',
# 设置网络代理
# 'proxy': {
# 'http': 'xx',
# 'https': 'xx'
# },
# 换成你的 bucket
'bucket': 'upload-*********',
# 换成 bucket 所在地区
'region': 'ap-chengdu',
# 这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的具体路径
# 例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
'allow_prefix': '*',
# 密钥的权限列表。简单上传和分片需要以下的权限,其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
'allow_actions': [
# 简单上传
# 'name/cos:PutObject',
'name/cos:PostObject',
# 分片上传
# 'name/cos:InitiateMultipartUpload',
# 'name/cos:ListMultipartUploads',
# 'name/cos:ListParts',
# 'name/cos:UploadPart',
# 'name/cos:CompleteMultipartUpload'
], } try:
sts = Sts(config)
response = sts.get_credential()
print('get data : ' + json.dumps(dict(response), indent=4))
except Exception as e:
print(e) return Response(response)

然后你访问网址

我们只需要把上面的网址放在开始咯处的网址那即可

上传成功


												

(十)微信小程序---上传图片chooseImage的更多相关文章

  1. 微信小程序wx.chooseImage和wx.previewImage的综合使用(图片上传可以限制个数)

    本例从微信小程序的组件扒下来的. WXML: <view class="weui-cell"> <view class="weui-cell__bd&q ...

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

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

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

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

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

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

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

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

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

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

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

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

  8. 微信小程序上传图片更新图像

    解决思路: 1. 调用wx.chooseImage 选择图片 2.wx.uploadFile 上传图片 3.调用后台接口进行修改操作 修改原来的头像 wx.chooseImage({ success: ...

  9. 微信小程序上传图片

    话不多说,直接上码. <view class="section"> <!--放一张图片或按钮 点击时去选择图片--> <image class='ph ...

随机推荐

  1. python爬虫(八) requests库之 get请求

    requests库比urllib库更加方便,包含了很多功能. 1.在使用之前需要先安装pip,在pycharm中打开: 写入pip install requests命令,即可下载 在github中有关 ...

  2. 「USACO5.5」矩形周长Picture

    题目描述 墙上贴着许多形状相同的海报.照片.它们的边都是水平和垂直的.每个矩形图片可能部分或全部的覆盖了其他图片.所有矩形合并后的边长称为周长. 编写一个程序计算周长. 如图1所示7个矩形. 如图2所 ...

  3. Python - 工具

    Anaconda - 自带Conda,可以自定义环境 Pycharm zeal - API离线查看,类似于Dash

  4. 1.项目创建及集成git

    1.创建项目 (1)创建完以后,打开Terminal命令行,输入"git init"用于初始化本地仓库 (2)打开对应的项目文件夹,“Ctrl+h”可以查看该文件夹里是否存在.gi ...

  5. iOS中常用的手势

    --前言 智能手机问世后的很长一段时间,各大手机厂商都在思考着智能手机应该怎么玩?也都在尝试着制定自己的一套操作方式.直到2007年乔布斯发布了iPhone手机,人们才认识到智能手机就应该这样玩. 真 ...

  6. CSS选择器整理

    基本选择器 标签选择器:直接写标签名 id选择器:#id名 class选择器:.class名 通配选择器:* 组合选择器 交集:ABCDEFG...... 并集:E, F, G, ...... 关系选 ...

  7. iOS 批量上传图片的 3 种方法

    AFNetworking 在去年年底升级到了 3.0.这个版本更新想必有很多好处,然而让我吃惊的是,它并没有 batch request 接口.之前的 1.x 版本.2.x 版本都实现了这个很常见的需 ...

  8. zookeeper和eureka的区别在哪?

    zookeeper和eureka的区别在哪?传统的关系型数据库是ACID(原子性,一致性,独立性,持久性), nosql数据库是CAP(强一致性,可用性,分区容错性),分布式系统只能3进2,三个选两个 ...

  9. CSP-S 2019 初赛游记

    Day 0 上午考了一套毒瘤的数据结构题,考的我心态爆炸SB出题人 晚上考了一套初赛模拟,只考1h,然后我91分,感觉初赛完全没问题? 回寝室后一直在忙活,整理东西什么的,居然将近12点睡? Day ...

  10. python3 使用selenium +webdriver打开chrome失败,报错:FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'

    提示chrome driver没有放置在正确的路径下 解决方法: 1.chromedriver与chrome各版本及下载地址 驱动的下载地址如下: http://chromedriver.storag ...