当前位置:网站首页>Applet /vant UI to upload files
Applet /vant UI to upload files
2022-06-22 07:36:00 【Shemo】
Applet /Vant UI Realize file upload
When I first came into contact with the small program upload , I have passed some experimental tests and recorded them for everyone to learn .
Use vant UI Official website :Vant Weapp - Light weight 、 Reliable small program UI Component library
How not to use UI There may be some differences under the premise of the framework , But it does not affect the overall implementation details .
Import codes on the official website :


The first time I look at the above code, I have some doubts , I don't know very well . Now I am also ignorant , I mainly know the functions implemented, but I don't pay attention to the details of the implementation .afterRead Is the callback function for successful upload ,fileList Is a component that allows images to be echoed to uploaded files , You can set a fixed URL To achieve echo .
The following code is a partial modification of the above code to call the interface provided by the background .
Page({
data: {
fileList: [],
},
afterRead(event) {
let that = this
const { file } = event.detail;
// When setting mutiple by true when , file In array format , Otherwise, it is in object format
wx.uploadFile({
url: 'http://127.0.0.3/upload_file.php', // The address of the interface
filePath: file.url,
name: 'file',
formData: { user: 'test' },
success(res) {
// The upload is completed and needs to be updated fileList
let fileList = []
let path = "http://127.0.0.3/" + res.data
fileList.push({ url: path });
console.log(fileList);
that.setData({ fileList: fileList });
// console.log(res.data);
},
});
},
});there res.data Is the file name I provided , Use the method of concatenating strings to provide url Here it is fileList assignment . After the assignment is completed, the echo of the picture can be realized , The implementation details are shown in the following figure :

So that we can resume the experiment , Show the interface code in the background for everyone to learn .
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
) // file size B && ($_FILES["file"]["size"] < 2000000)
{
if ($_FILES["file"]["error"] > 0) // error
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else // Upload files to the temporary system
{
//echo "Upload: " . $_FILES["file"]["name"] . "<br />";
//echo "Type: " . $_FILES["file"]["type"] . "<br />";
//echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
//echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"])) // File exists
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else // Move the file to the specified location
{
// get UUID
$uuid = uuid();
$uuid = str_replace('-','',$uuid);
$extension = explode(".",$_FILES["file"]["name"] );
$key = count($extension) - 1;
$suffix = $extension[$key];
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $uuid.".".$suffix);
echo "upload/" . $uuid.".".$suffix; //Stored in: " . "upload/" . $uuid.$suffix
//echo "Stored in: " . "upload/". $uuid.".".$suffix;
}
}
}
else
{
echo "Invalid file"; // Wrong file type
}
function uuid()
{
$chars = md5(uniqid(mt_rand(), true));
$uuid = substr ( $chars, 0, 8 ) . '-'
. substr ( $chars, 8, 4 ) . '-'
. substr ( $chars, 12, 4 ) . '-'
. substr ( $chars, 16, 4 ) . '-'
. substr ( $chars, 20, 12 );
return $uuid ;
}
?>边栏推荐
- Propeller framework v2.3 releases high reusable operator library Phi: Restructure development paradigm, reduce cost and increase efficiency
- 微信小游戏(三)
- Device tree failed to add SPI device node
- Daily maintenance of website
- How to import and upload a CSV generated by a third-party platform to a Taobao store
- How to upload Taobao tmall products with one click
- Set the way that liquid (Jekyll) is displayed in markdown but not parsed
- Application and problem solving of robotframework
- Simplicity is the best method of network promotion
- Open version - user level
猜你喜欢

Crmeb open version 4.3

Phpcms mobile portal configuration

Canoe learning notes (6) diagram of logging block data recording module

Kuangshi brain++ Tianyuan platform megstudio

记一次内网渗透测试实训总结

Matlab suddenly fails to open. It disappears after running. There is no solution for the task manager

Tikz learning notes (IV) further exploration of circle and complex geometry

Image interpolation (nearest neighbor, bilinear)

Can Taobao batch copy babies synchronize the original baby inventory

The pit on the school recruitment Road
随机推荐
Taobao store backup one store upload to multiple stores precautions
网站是否要修改标题
Wechat applet service provider sub merchant payment order interface
Target detection series -- detailed explanation of RCNN principle
JS implementation of random generation of 16 bit key -- Basic accumulation
Find and replace the spaces, Nan and special symbols contained in a column of data in the dataframe
Do you want to modify the title of the website
How to backup the treasures in the store and upload them to multiple stores
Unity AssetBundle packaging
Wechat games (3)
Backup the method of uploading babies in Taobao stores to multiple stores
网站的排名对于网站很重要
Téléchargement de toutes les versions de chromedriver
How to improve Baidu's weight
antd 框架:点击链接重开浏览器页面——基础积累
微信小游戏(二)
Qualcomm platform msm8953 display subsystem learning
What if the finder fails to respond continuously? Finder unresponsive solution tutorial
Docker command, docker installation sqlserver2019, docker installation MySQL (continuous update)
Shutter margin negative margin