如何使用Angular中的块将大文件上传到服务器

发布于 2025-02-02 17:23:06 字数 68 浏览 4 评论 0原文

我想将一个大文件汇总并将其上传到服务器 如何使用块文件上传和rxjs在Angular 2中将发布请求发送到服务器的大型文件

I want to chunk a large file and upload it to the server
how to send a post request to a large file to a server using chunk file upload and RxJS in angular 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菩提树下叶撕阳。 2025-02-09 17:23:07

我使用JavaScript将文件上传到需要令牌的API。

<button id="upload-button" onclick="uploadFile()"> Upload </button>

在索引文件中使用此脚本:

<script type='text/javascript'>
    async function uploadFile() {
    let formData = new FormData();      
    formData.append("token", window.sessionStorage.getItem("token"));      
    formData.append("file", fileupload.files[0]);
    console.log("Name " +formData.get("file").name);
    console.log("Token " +formData.get("token"));

    const response = await fetch('http://localhost:8080/upload', {
      method: "POST", 
      mode: 'no-cors',
      credentials: 'omit',
      headers: {
        'Content-Type': 'application/json',
      },
      body: formData
    });

    console.log(response);
    if(response){
      console.log("File uploaded");
      alert('The file has been uploaded successfully.');
    } else {
      throw new Error('Network response was not OK');
    }
}
</script>

I used a javascript to upload the file to an api which needs a token.

<button id="upload-button" onclick="uploadFile()"> Upload </button>

Use this script in the index file:

<script type='text/javascript'>
    async function uploadFile() {
    let formData = new FormData();      
    formData.append("token", window.sessionStorage.getItem("token"));      
    formData.append("file", fileupload.files[0]);
    console.log("Name " +formData.get("file").name);
    console.log("Token " +formData.get("token"));

    const response = await fetch('http://localhost:8080/upload', {
      method: "POST", 
      mode: 'no-cors',
      credentials: 'omit',
      headers: {
        'Content-Type': 'application/json',
      },
      body: formData
    });

    console.log(response);
    if(response){
      console.log("File uploaded");
      alert('The file has been uploaded successfully.');
    } else {
      throw new Error('Network response was not OK');
    }
}
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文