Let's talk about Uniapp's photo upload and delete operations
In recent years, Uniapp has become the first choice for more and more developers in the field of mobile application development. Uniapp is a new development framework through which developers can develop multi-terminal applications, thus improving the development efficiency of engineers. This article will provide a detailed introduction and explanation of Uniapp’s photo upload and delete operations.
1. Implementation of picture upload
Camera and picture selection are one of the common functions. Uniapp provides a rich API interface to enable cameras, photo albums, WeChat Moments, online files, etc. Multiple ways of selecting and uploading images are possible. Below we will introduce in detail how Uniapp's API interface implements the image upload function.
- Select pictures and upload
Uniapp provides a very easy-to-use component, uni-upload, which can upload files asynchronously, and then select through uni-upload Image upload function.
First add the following code on the front-end page:
<view> <uni-upload :upload-url="'your_upload_url'" :on-success="success" :on-fail="fail" @click="upload"> <view class="button">上传图片</view> </uni-upload> </view>
In this code, we define a uni-upload
component, in which upload-url# The ## attribute is the URL address of the image upload,
on-success and
on-fail correspond to the callback functions for upload success and failure respectively.
@clickThe attribute triggers the upload function after clicking.
success and
fail in the Vue instance:
methods: { success(res){ console.log("上传成功"); }, fail(err){ console.log("上传失败"); }, upload(){ uni.chooseImage({ sizeType: ['compressed'], sourceType: ['album', 'camera'], success: (res) => { const tempFilePaths = res.tempFilePaths; uni.uploadFile({ url: this.uploadUrl, filePath: tempFilePaths[0], name: 'file', success: (res) => { this.success(res) }, fail: (err) => { this.fail(err) } }); } }); } }
success and
fail. When the upload succeeds or fails, the corresponding callback function will be executed. In the
upload function, we use the uni.chooseImage method to select the image, obtain the temporary file path, and use the uni.uploadFile method to upload the file to the server. The
name attribute represents the key value corresponding to the file, that is, the parameter name of the file received on the server.
- Upload pictures and get the return results
uni.uploadFile interface, we add a parameter in the success callback function to receive the result returned by the backend after the upload is successful. The modified code is as follows:
methods: { success(res){ const data = res.data; console.log(data); console.log("上传成功"); }, fail(err){ console.log("上传失败"); }, upload(){ uni.chooseImage({ sizeType: ['compressed'], sourceType: ['album', 'camera'], success: (res) => { const tempFilePaths = res.tempFilePaths; uni.uploadFile({ url: this.uploadUrl, filePath: tempFilePaths[0], name: 'file', success: (res) => { this.success(res); }, fail: (err) => { this.fail(err); } }); } }); } }
success.
- Use the uni.removeSavedFile method to delete pictures
methods:{ deleteImage(index) { const filePath = this.uploadList[index].filePath; uni.removeSavedFile({ filePath: filePath, success(res) { console.log(res) }, fail(err) { console.log(err) } }); } }
<view v-for="(item,index) in uploadList" :key="index"> <image :src="item.filePath" mode="aspectFit" style="width:50px;height:50px"></image> <view v-on:click="deleteImage(index)">删除</view> </view>
deleteImage method is used to delete the corresponding file.
- Initiate a deletion request to the server
methods:{ deleteImage(index) { const url = 'your_delete_url'; const fileID = this.uploadList[index].url; uni.request({ url: url, method: 'DELETE', data:{ fileID:fileID, key:'value' // 可以添加其他参数 }, success: (res) => { console.log(res); }, fail: (err) => { console.log(err); } }); } }
The above is the detailed content of Let's talk about Uniapp's photo upload and delete operations. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
