nodejs怎么模拟input type=file拿到filelist

nodejs怎么得到html中点击<input type="file">选中文件后得到的filelist

哆啦的时光机
浏览 1341回答 1
1回答

函数式编程

TALK IS CHECAP, SHOW ME THE CODE<html><body>&nbsp; <form>&nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; <label>Select file to upload</label>&nbsp; &nbsp; &nbsp; &nbsp; <input type="file">&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <button id="taozhi" type="submit">Convert</button>&nbsp; &nbsp; </form>&nbsp; <script>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var p = document.getElementById("taozhi");&nbsp; &nbsp; &nbsp; &nbsp; p.onclick = showAlert;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; function showAlert(event) {&nbsp; &nbsp; &nbsp; &nbsp; // 这就是你需要的吧!!input.files获取选中文件之后的filelist&nbsp; &nbsp; &nbsp; &nbsp; const input = document.querySelector('input[type="file"]')&nbsp; &nbsp; &nbsp; &nbsp; const file = input.files[0]&nbsp; &nbsp; &nbsp; &nbsp; let formData = new FormData();&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; formData.append('image', file);&nbsp; &nbsp; &nbsp; &nbsp; fetch('http://localhost:8001/upload',{&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method:'post',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body:formData,&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; .then((response) => response.json() )&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; .then((response)=>{&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(response)&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; .catch((err)=>console.error(err));&nbsp;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript