3

我必须将一个大文件拆分为 2mb 的部分才能发送到服务器,但我找不到任何方法。在角度或javascript上。现在我正在使用 angularFileUpload 来获取它并将其作为一个大文件发送。如果有人有线索请告诉我

4

3 回答 3

4

您必须使用 HTML5 文件 API。您可以在此处找到有关它的更多信息。我无法提供任何代码示例,主要是因为我不知道您的服务器的外观。你必须给用户一个交易令牌,他必须向你发送块号、块数据和令牌,这样你就可以在服务器上重新组装它。

于 2015-01-14T16:49:58.473 回答
0

您应该能够使用FileAPI。我相信它为不支持 HTML5 File API 的旧浏览器提供了一个 shim。

这是在angular-file-upload repo中使用的示例

于 2015-01-14T18:16:40.483 回答
0

您可以尝试以下代码,这可能会帮助您将文件读入块中。在 HTML 文件中。这里 Filereader 是作为文本读取的,但我们可以选择其他方式,比如作为缓冲区读取等。

并在 ts 文件中

uploadDoc(event) {
    let lastChunksize = 0;
    var file = event.target.files[0];

    this.readFile(file, lastChunksize, this.myCallback.bind(this));

   }

   myCallback(file, lastChunksize, result) {
    lastChunksize = lastChunksize + 20000;
    if(result) {
      //Add you logic what do you want after reading the file
      this.readFile(file, lastChunksize, this.myCallback.bind(this));
    } else {
      ///end recursion
    }
   }

   readFile(file,lastChunksize: number, callback) {

    var fileBlob = file.slice(lastChunksize,lastChunksize+20000);
    if(fileBlob.size !=0) {
      let fileReader = new FileReader();
      fileReader.onloadend= (result)=>{
      return callback(file,lastChunksize,fileReader.result)
      }
      fileReader.readAsText(fileBlob);
    }else {
     return callback(file,lastChunksize,false);
    }
   }
于 2019-10-04T14:22:08.197 回答