如何使用node js multer将大文件上传到linode服务器
大家好,我有一个 linode 服务器,我正在尝试通过端点将文件上传到我的 linode 服务器上托管的后端服务器,当我上传小于 1-2 mb 的文件时,它可以工作,但超过它就不行,我尝试过使用heroku测试相同的上传功能,它可以工作,但它在我的linode服务器上不起作用,而是我的axios返回“网络错误”,请问我需要做什么配置才能允许使用react axios、node js和穆特。
我的代码
try{
const formData = new FormData();
formData.set('txt', textValue)
formData.set('post_for', PostFor)
fileList.forEach(element => {
formData.append('files', element)
})
setloading(true)
setError();
axios.post(`${ENDPOINT}/feed/`, formData,{
headers:{token:localStorage.token,"Content-Type":"multipart/form-data"},
onUploadProgress: (data) => {
setProgress(Math.round(100 * (data.loaded / data.total)));
},
}).then(res => {
if(res.data){
setloading(false)
setFileList([])
setpreviewImage([])
setpreviewVideo([])
setText('')
setError();
socket.emit('refresh_feed')
message.success('feed uploaded successfully')
setTimeout(()=>{
setProgress()
},6000)
}else if(res.error){
setloading(false)
message.error(res.error)
}
}) .catch((error) => {
const code = error
switch (code) {
case "FILE_MISSING":
setError("Please select a file before uploading");
setloading(false)
break;
case "LIMIT_FILE_SIZE":
setError("File size is too large. Please upload files below 1MB!");
setloading(false)
break;
case "INVALID_TYPE":
setError(
"This file type is not supported. Only .png, .jpg, and .jpeg files are allowed"
);
setloading(false)
break;
default:
setError("Sorry, something went wrong");
setloading(false)
break;
}
});
}catch (error) {}
}
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, `./src/uploads`)
},
filename: function (req, file, cb) {
// if(file.mimetype === 'video/mp4' || file.mimetype === 'audio/mpeg'){
// return cb(null, Date.now() + '.mp3')
// }
cb(null, Date.now() + path.extname(file.originalname))
}
})
export const upload = multer({ storage: storage })
Hi guys i got a linode server and am trying to make a file upload through an endpoint to my backend server hosted on my linode server, when i upload files less than 1-2 mb it works but above that it doesn't, i tried testing same upload functionality using heroku it worked, but it doesn't work on my linode server instead my axios returns a "network error" please is there any config i need to do to allow large file upload am using react axios, node js and multer.
my code
try{
const formData = new FormData();
formData.set('txt', textValue)
formData.set('post_for', PostFor)
fileList.forEach(element => {
formData.append('files', element)
})
setloading(true)
setError();
axios.post(`${ENDPOINT}/feed/`, formData,{
headers:{token:localStorage.token,"Content-Type":"multipart/form-data"},
onUploadProgress: (data) => {
setProgress(Math.round(100 * (data.loaded / data.total)));
},
}).then(res => {
if(res.data){
setloading(false)
setFileList([])
setpreviewImage([])
setpreviewVideo([])
setText('')
setError();
socket.emit('refresh_feed')
message.success('feed uploaded successfully')
setTimeout(()=>{
setProgress()
},6000)
}else if(res.error){
setloading(false)
message.error(res.error)
}
}) .catch((error) => {
const code = error
switch (code) {
case "FILE_MISSING":
setError("Please select a file before uploading");
setloading(false)
break;
case "LIMIT_FILE_SIZE":
setError("File size is too large. Please upload files below 1MB!");
setloading(false)
break;
case "INVALID_TYPE":
setError(
"This file type is not supported. Only .png, .jpg, and .jpeg files are allowed"
);
setloading(false)
break;
default:
setError("Sorry, something went wrong");
setloading(false)
break;
}
});
}catch (error) {}
}
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, `./src/uploads`)
},
filename: function (req, file, cb) {
// if(file.mimetype === 'video/mp4' || file.mimetype === 'audio/mpeg'){
// return cb(null, Date.now() + '.mp3')
// }
cb(null, Date.now() + path.extname(file.originalname))
}
})
export const upload = multer({ storage: storage })
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论