1. Select the image to the Node server

Uni – app code

uni.chooseImage({
    success: (res) = > {
        let tempFilePaths = res.tempFilePaths;
        tempFilePaths.forEach((item) = > {
            uni.uploadFile({
                url: ' '.// Server address
                fileType: "image".//ZFB must be filled, otherwise error
                filePath: item, // This is the array we returned from the photo above
                name: 'file'.success: (uploadFileRes) = >{}}); }}}));Copy the code

Node service code

const path = require("path"); // Import path module
const multer = require("multer"); // Import the multer module
const fs = require("fs"); // Import file operation module
const util = require(".. /public/util");
const upload = multer({ dest: "tmp/" });
let express = require("express");
let router = express.Router();
router.post("/upload", upload.single("file"), function (req, res) {
  let imgFile = req.file; // Get a resource for uploading images
  var tmp = imgFile.path; // Get temporary resources
  let ext = path.extname(imgFile.originalname); // Use the path module to get the suffix of the image uploaded by the user
  let newName =util.getNowFormatDate(The '-'.The '-') +"-"+new Date().getTime() + Math.round(Math.random() * 10000) + ext; // Rename images uploaded by users to prevent duplication
  let newPath = "/public/images/" + newName; Create an images folder !!!! in advance under the current folder
  let fileData = fs.readFileSync(tmp); // Load temporary resources uploaded to the server into a variable
  let filePath = path.join(__dirname, '.. '+newPath);
  fs.writeFileSync(filePath, fileData); // Re-write the image file to the specified folder
  res.send(newPath); // Reply to client after successful upload
});

router.get('/public/images/*'.function (req, res) {
    let newPath = ".."+req.url;
    let filePath = path.join(__dirname,newPath);
    res.sendFile(filePath);
})

module.exports = router;

Copy the code

2, select the image to the cloud storage

uni.chooseImage({
    success: (res) = > {
        let tempFilePaths = res.tempFilePaths;
        tempFilePaths.forEach((item) = > {
           uniCloud.uploadFile({
                filePath: item,
                cloudPath: 'item.jpg'.success(res){},fail(res){},complete(){}}); }}}));Copy the code