gpt4 book ai didi

node.js - node.js的multer中的mp4文件上传问题

转载 作者:行者123 更新时间:2023-12-04 23:20:51 25 4
gpt4 key购买 nike

我正在尝试通过在 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("비디오 업로드를 실패 했습니다.");
}
});
};

}
(服务器)
视频.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, "上传/")
} <----- 我可以使用 console.log(response.data) 检查要上传的文件,但是“uploads”文件夹中没有 mp4 文件。
当我将“uploads/”更改为我的本地目录路径时,mp4 文件位于 uploads 文件夹中......
例如:“C://~~~/uploads”
任何想法?

最佳答案

使用 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);
},
});

关于node.js - node.js的multer中的mp4文件上传问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66713138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com