1

我使用 Uppy 作为我的拖放文件上传,但我们都知道 Uppy 并不负责将其实际上传到服务器。因此,我使用 Multer 作为将文件上传到服务器的后端。虽然,我遇到了障碍。当我上传文件时,我已经将文件名更改为当前日期加上扩展名。但是当我控制台记录它时,它会出现“未定义”。

函数.js

let path = require('path');
const multer = require('multer');



let myUploadFunction = function _multerFunction(req, res, filePath){


    let newFileName;

    let storage = multer.diskStorage({
        destination: function (req, file, callback) {
            callback(null, filePath);
        },
        filename: function (req, file, callback) {
            console.log("Storage Function " , file);
            callback(null, newFileName = Date.now() + path.extname(file.originalname));

            getNewFileName(newFileName); // Trying to pass this function to the upload.js route but it comes back undefined.

        }
    });

    let upload = multer({ storage : storage}).any();

    upload(req, res, function (err) {
        if(err) {
            return res.end("Error uploading file. "+ err);
        }
        console.log("New File Name " + newFileName); // This console log does print the new file name

    });

};

let getNewFileName = function getNewCreatedFileName(fileName){
   return fileName;
};

module.exports.myUploadFunction = myUploadFunction;
module.exports.getNewFileName = getNewFileName;

上传.js

let express = require('express');
let router = express.Router();

let upload = require('../functions/functions');

//const mysql_connection = require('../db'); // Database connection file.

/* POST upload route. */
router.post('/', function(req, res, next) {

  upload.myUploadFunction(req, res, 'public/images', 'File is upload successfully');

   console.log("From the upload route " + upload.getNewFileName()); // RETURNS UNDEFINED
});

module.exports = router;

我不知道为什么它会返回未定义。我正在传递函数。我还缺少其他东西吗?

我还包含了 uppy 代码,您需要查看它。

uppy.js

// Import the plugins
const Uppy = require('@uppy/core');
const XHRUpload = require('@uppy/xhr-upload');
const Dashboard = require('@uppy/dashboard');


const uppy = Uppy({
    debug: true,
    autoProceed: false,
    restrictions: {
        maxFileSize: 1024000,
        maxNumberOfFiles: 3,
        minNumberOfFiles: 1,
        allowedFileTypes: ['image/*', 'video/*']
    }
})
    .use(Dashboard, {
        trigger: '.UppyModalOpenerBtn',
        inline: true,
        target: '#drag-drop-area',
        replaceTargetContent: true,
        showProgressDetails: true,
        proudlyDisplayPoweredByUppy: false,
        animateOpenClose: true,
        note: 'Images and video only, 1–3 files, up to 1 MB',
        height: 470,
        browserBackButtonClose: true,
        theme: 'dark',
        metaFields: [
            {id: 'caption', name: 'Caption', placeholder: 'describe what the image is about'}
        ]
    });

uppy.on('file-added', (file) =>{
    console.log(file);
    uppy.setFileMeta(file.meta.id, {
       caption: file.name
    });
});

uppy.use(XHRUpload, {

    id: 'XHRUpload',
    endpoint: 'http://localhost:8000/upload',
    method: 'POST',
    formData: true,
    fieldName: 'my_fieldName',
    metaFields: ['caption'],

});

uppy.on('upload-success', (file, response) => {

    console.log("File uploaded successfully ", file);

});

module.exports = uppy;

如果您需要查看其他代码,请告诉我。谢谢!

顺便说一句,我正在使用 browserify 来实现 Uppy。:)

如果 Multer 不是使用 Uppy 将文件上传到服务器的最佳方式,我很乐意采取任何更好的选择。谢谢!

4

0 回答 0