【问题标题】:Split a big file to be upload using angular使用角度拆分要上传的大文件
【发布时间】:2015-03-12 22:05:43
【问题描述】:

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

【问题讨论】:

标签: javascript angularjs angular-file-upload


【解决方案1】:

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

【讨论】:

  • 我正在阅读 FileApi 我希望所有这些都可以被 apiRest 支持,因为在服务器端我有一个带有控制器的 c# 应用程序
  • 太棒了!您可以找到一个接受分块上传的服务器示例here
  • 嗯,我有 ie6 + 所以可以使用 blob...你有什么办法吗?
  • 抱歉回复晚了。如果你打算以 IE6+ 为目标,那你就有点“注定”了。 IE6/7/8 不提供执行此操作的 API。您应该考虑使用 Adob​​e Flash,或者,因为您已经了解 C#,所以 Silverlight :(
  • 我们的客户现在使用 IE9+,所以我需要找到一个接受分块上传的服务器示例
【解决方案2】:

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

这是example 中使用的angular-file-upload repo

【讨论】:

  • 我正在阅读 FileApi 我希望所有这些都可以被 apiRest 支持,因为在服务器端我有一个带有控制器的 c# 应用程序
【解决方案3】:

您可以尝试以下代码,这可能有助于您将文件读入块中。 在 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);
    }
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-07
    • 2022-12-24
    • 2014-11-16
    • 2021-08-21
    • 2022-01-05
    • 2018-06-04
    • 2016-11-09
    • 2017-02-21
    相关资源
    最近更新 更多