简体   繁体   English

reactjs - 如何在没有提交按钮的情况下上传文件

[英]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.

相关问题 如何使上传按钮提交文件而无需第二个提交按钮php Symfony3 - How to make upload button submit files without second submit button php Symfony3 单击表单提交按钮后,如何在反应 js 中关闭 model? - How can i close model in react js after i click form submit button? 如何在不使用提交按钮的情况下上传图像? - How to upload image without using submit button? 如何在没有提交按钮的情况下上传文件? - How to upload a file without the submit button? 我可以使用提交按钮和JS提交表单吗? - can i submit form with submit button and JS? 如何在没有提交按钮的情况下检查单选按钮是否被选中? - How can i check if the radio button is checked or not without a submit button? 我如何使用这个由 vanila js 制作的自定义文件上传按钮到 React 应用程序中? - How can I use this custom file upload button which is made by vanila js into React application? 仅当单击提交按钮时,如何让 Dropzone.js 上传文件? - How to get Dropzone.js to upload files only when a submit button is clicked? 如何将提交按钮连接到反应中的 2 个输入? - How can i connect a submit button to 2 inputs in react? 如何在 html 表单中使用 JS function 控制提交按钮 - how can i control the submit button with JS function in html form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM