如何在react app中单击按钮将文件上传到服务器?

编程入门 行业动态 更新时间:2024-10-09 10:23:41

如何在react app中<a href=https://www.elefans.com/category/jswz/34/1771345.html style=单击按钮将文件上传到服务器?"/>

如何在react app中单击按钮将文件上传到服务器?

我已经构建了一个React应用程序,其中我在首页中渲染了主页。现在,我想在导航栏中嵌入文件上传功能。所以我在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 /
    -->
    <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节点模块,它可以帮助您将文件上传到服务器的特定路径

这里是您可以检查其使用方式的链接:-Resource for multer或File upload with multer

更多推荐

如何在react app中单击按钮将文件上传到服务器?

本文发布于:2024-05-07 15:04:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756827.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   文件上传   按钮   服务器   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!