The official documentation


https://developers.weixin.qq….

index.wxml

<view class="container"> <button bindTap ="imgupload"> </button> </view>

index.js

const app = getApp() Page({ data: {}, Imgupload (){wx.chooseImage({success (res) {// Get selected pictures const TempFilepaths = Res.TempFilepaths // Upload every selected picture for (var i = 0; i < tempFilePaths.length; I ++) {wx.uploadFile({url: 'Your upload server HTTPS interface ', filePath: TempFilepaths [I], name: 'file', success (res){ const msg = JSON.parse(res.data).msg; const url = JSON.parse(res.data).url; const code = JSON.parse(res.data).code; If (json.parse (res.data). Code == 200){wx.showToast({title: 'success', icon: 'success', duration: }) console.log(url)}else{wx.showToast({title: 'Upload failed ', icon: 'error', duration: 1000 }) } console.log(msg) } }) } } }) } })

index.php

<? php header("Content-Type:application/json"); $allowedExts = array("jpeg", "JPG "," PNG "); $allowedExts = array("jpeg", "PNG "); / / suffix if ($allowedExts [0] = = 'jpeg') {$HZM = 'JPG'; }else{ $hzm = $allowedExts[0]; } / / access to select file $temp = explodes (". ", $_FILES/" file "/" name "); $extension = end($temp); 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")) && ($_FILES [" file "] [" size "] < 10485760) / / maximum can upload 10 MB && in_array ($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { $result = array( 'code' => 201, $_FILES["file"]["error"]); $_FILES["file"]["error"]); } else {// Upload the file to the current Upload directory // If there is no Upload directory, you need to create it. If (file_exists("upload/". $_FILES["file"]["name"])) {$result = array('code' => 202); $result = array('code' => 202); 'MSG' => 'file exists'); $new_file = date("Y-m-d")."-". Rand (10000,99999).".".$HZM; move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$new_file); $result = array('code' => 200, 'MSG' => ', 'url' => '); $result = array('code' => 200, 'MSG' => '); }} else {$result = array('code' = BBB 0, 'MSG' = BBB 0, 'MSG'); } echo json_encode($result,JSON_UNESCAPED_UNICODE); ? >

Directions for use

(1) The back-end code needs to modify the path where your code is uploaded to the server in order to display the address of the successfully uploaded picture normally. For example, if your code is uploaded to the IMG directory under the root directory of the server, Then you need to change the path to http:// domain name /img/upload (2) and create a new folder called Upload in the same directory as index.php. This folder is used to store the uploaded image file.

Author:TANKING

Date:2021-04-14

WeChat:sansure2016

Web:http://www.likeyuns.com