[英]How to upload a file to the server on a button click in react app?
我已經構建了一個反應應用程序,其中我在首頁呈現主頁。 現在我想在導航欄中嵌入文件上傳功能。 所以我在 public/index.html 中添加了按鈕。 我有一個服務器和客戶端。 服務器在 localhost:8000 上運行。 現在,如果我想將文件上傳到服務器中的特定路徑,假設服務器中有一個名為“example-file”的文件夾。 我怎樣才能做到這一點? 任何幫助將非常感激。 附上索引文件和我的項目結構截圖。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/fevicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>Config Genrator</title>
</head>
<body class="d-flex flex-column min-vh-100 bg-light">
<noscript>You need to enable JavaScript to run this app.</noscript>
<nav
class="navbar navbar-light"
style="width: 100%;background-image: linear-gradient(to right,White,#333);"
>
<a class="navbar-brand" href="#">
<img
src="%PUBLIC_URL%/logo.png"
width="162"
height="39"
class="d-inline-block align-top"
alt=""
/>
</a>
<span class="navbar-brand text-white">
Configuration Genrator
</span>
<input type="button" class="fas fa-file-upload" id="btnFileUpload" value="Upload File" />
</nav>
<div
id="root"
class="d-inline-flex align-self-center"
style="width: 90%;"
></div>
<footer style="width: 60%;" class="d-inline-flex align-self-center mt-auto">
<p class="text-center" style="width: 100%;">©2020 </p>
</footer>
</body>
</html>
嘗試使用multer node 模塊,它可以幫助您將文件上傳到服務器的特定路徑
這是您可以檢查如何使用它的鏈接:- multer 資源或使用 multer 上傳文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.