【问题标题】:mp4 file upload issue in multer of node.jsnode.js的multer中的mp4文件上传问题
【发布时间】:2021-06-17 03:43:32
【问题描述】:

我正在尝试使用 node.js 中的 multer 上传 mp4 文件

(查看)VideoUploadPage.js

import React, { useState } from "react";
import Axios from "axios";


function VideoUploadPage() {

const onDrop = (files) => {
    let formData = new FormData();
    const config = {
      header: { "content-type": "multipart/form-data" },
    };
    formData.append("file", files[0]);
    console.log(files);

    Axios.post("/api/video/uploadfiles", formData, config).then((response) => {
      if (response.data.success) {
        console.log(response.data);
      } else {
        alert("비디오 업로드를 실패 했습니다.");
      }
    });
  };

}

(服务器)

video.js

const express = require("express");
const router = express.Router();
const multer = require("multer");
const path = require("path");
const ffmpeg = require("fluent-ffmpeg");



let storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, "uploads/");
  },
  filename: (req, file, cb) => {
    cb(null, `${Date.now()}_${file.originalname}`);
  },
  fileFilter: (req, file, cb) => {
    const ext = path.extname(file.originalname);
    if (ext !== ".mp4") {
      return cb(res.status(400).end("only mp4 is allowed"), false);
    }
    cb(null, true);
  },
});

const upload = multer({ storage: storage }).single("file");

router.post("/uploadfiles", (req, res) => {
  upload(req, res, (err) => {
    if (err) {
      return res.json({ success: false, err });
    }
    return res.json({
      success: true,
      url: res.req.file.path,
      fileName: res.req.file.filename,
    });
  });
});

在 video.js 目的地:(req, file, cb) => { cb(null, "上传/") }

当我将“uploads/”更改为我的本地目录路径时,mp4 文件位于 uploads 文件夹中......

例如:“C://~~~/uploads”

有什么想法吗?

【问题讨论】:

    标签: node.js express ffmpeg multer


    【解决方案1】:

    你可以使用 path.resolve(process.cwd() + 'path until upload folder');

    例如: path.resolve(process.cwd() + '/uploads');

    【讨论】:

    • 也不行。我正在尝试不同的方式
    • "../uploads"
    • 在我看来,上述解决方案是有效的。您可能提供了错误的上传文件夹路径。我猜这就是为什么它对你不起作用的原因。
    • 其实它就像一个教程的东西。我搜索了其他人这样做,但代码完全相同
    【解决方案2】:

    使用 path.resolve('./uploads') 将一系列路径段解析为绝对路径。

    let storage = multer.diskStorage({
    destination: (req, file, cb) => {
    cb(null, path.resolve('./uploads'); // path of the upload folder
    },
    filename: (req, file, cb) => {
    cb(null, `${Date.now()}_${file.originalname}`);
    },
    fileFilter: (req, file, cb) => {
    const ext = path.extname(file.originalname);
    if (ext !== ".mp4") {
      return cb(res.status(400).end("only mp4 is allowed"), false);
    }
    cb(null, true);
    },
    });
    

    【讨论】:

    • 我已经试过了,但是不行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    • 2017-10-20
    • 2016-05-22
    • 2017-12-03
    • 2017-05-22
    • 1970-01-01
    • 2017-12-26
    相关资源
    最近更新 更多