[英]How can I upload files without submit button in react js
I have an input and submit button for uploading multiple files via axios and multer.我有一个输入和提交按钮,用于通过 axios 和 multer 上传多个文件。 It works but I want to upload my files without submit button.它可以工作,但我想在没有提交按钮的情况下上传我的文件。 So it should upload them immediately after I choose my files.所以它应该在我选择我的文件后立即上传它们。
These are the codes... I tried to use callback methods but couldn't figure out how to syntax should be.这些是代码......我尝试使用回调方法,但无法弄清楚语法应该如何。 Is there a way to call uploadHandler function after upload function.上传function后有没有办法调用uploadHandler function。 I can't simply merge them because state gives me undefined value.我不能简单地合并它们,因为 state 给了我未定义的值。 * I use functional components not classes * 我使用功能组件而不是类
function upload (e) {
const inputArr = new Array()
Array.from(e.target.files).forEach((piece)=>{
inputArr.push(piece.name)
})
setInputArray({inputArray: inputArr})
setInputFiles({inputFiles:e.target.files})
}
After this, this should run在此之后,这应该运行
function uploadHandler () {
const fd = new FormData();
if(inputFiles!=null)
{
for ( var element of inputFiles.inputFiles) {
console.log(element)
fd.append('file',element)
}
}
axios.post('/upload', fd)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
I invoke them with these我用这些调用它们
<input type="file" id="file" name="file" multiple="multiple" onChange={upload}/>
<button onClick={uploadHandler}>Send Files</button>
Since your arrays states are not updated in the instant when you set setInputArray({inputArray: inputArr}), directly calling uploadHandler make arrays null.由于您的 arrays 状态在设置 setInputArray({inputArray: inputArr}) 时不会立即更新,因此直接调用 uploadHandler 使 arrays null。
To acheive this, you need useEffect !要实现这一点,您需要 useEffect !
import React, { useState, useEffect } from 'react';
useEffect(() => {
uploadHandler(inputFiles);
}, [inputFiles]);
And you have to modify your uploadHandler function déclaration as:您必须将您的 uploadHandler function 声明修改为:
function uploadHandler(files) {
let fd = new FormData(); // can't be a const if you want to add some key:value
if(files!=null)
{
for ( var element of files.inputFiles) {
console.log(element)
fd.append('file',element)
}
axios.post('/upload', fd)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
else
{
console.log("Nothing to upload");
}
}
You can try to create a hidden input after you click something like download button.单击下载按钮之类的内容后,您可以尝试创建隐藏输入。 It will open window to choose files and immediately make you actions with it:它将打开 window 以选择文件并立即让您使用它:
let file = document.createElement('input');
file.type = 'file';
file.multiple = true;
file.style.display = 'none';
document.body.appendChild(file);
file.click();
file.onchange = (e) => {
e.stopPropagation();
e.preventDefault();
let files = e.target.files;
//Here you have you files
//Here you can make anything with it
};
It's an example with pictures, but method with hidden input is universal这是一个带有图片的示例,但是带有隐藏输入的方法是通用的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.