This article mainly introduces how to use a mini-app to obtain images from an album-using a camera to take photos and uploading local images. It has good reference value. Next, let's take a look at it. This article mainly introduces how to use a mini-program to get images from an album-use a camera to take photos and upload local images. It has good reference value. Let's take a look at it with the small editor.
Today, I met with the user profile picture setting function of the mini program and took notes.
First Upload gif:
Run the following code:
Small demo, the code is very simple.
1. index. wxml
Get image
2. index. js
// Index. js // Obtain the application instance var app = getApp () Page ({data: {tempFilePaths: ''}, onLoad: function () {}, chooseimage: function () {var _ this = this; wx. chooseImage ({count: 1, // Default 9 sizeType: ['original', 'compressed '], // you can specify whether the source image or the compressed image is used. Both have sourceType by default: ['alipay', 'Camera '], // you can specify whether the source is an album or a camera. Both have success: function (res) by default) {// return the local file path list of the selected image. the tempFilePath can be used as the src attribute of the img label to display the image _ this. setData ({tempFilePaths: res. tempFilePaths })}})}})
API description:
Here we will talk about sourcetype. by default, it is used to get and take photos from the album. like the image selection interface, the first line is to take photos, followed by the album photos.
Note: The image is returned in the local path. if you need to upload the image to the server, you need to use another API.
Sample code:
Wx. chooseImage ({success: function (res) {var tempFilePaths = res. tempFilePaths wx. uploadFile ({url: 'http: // example.weixin.qq.com/upload', // only for example. The non-real interface address is filePath: tempFilePaths [0], name: 'file', formData: {'user': 'test'}, success: function (res) {var data = res. data // do something }})}})
The above describes how to develop the camera and local image upload functions. For more information, see other related articles on php Chinese network!