【问题标题】:Downloading and merging multipart file in javascript在javascript中下载和合并多部分文件
【发布时间】:2013-02-23 15:40:47
【问题描述】:

我需要一些关于以下要求的帮助。

  • 从网络服务器下载巨大的多部分 csv 文件,并在客户端将这些部分加入,然后再为用户打开。
  • 我可以在下载之前从网络服务器获取部件的数量和它们的 URL。
  • 我需要在客户端浏览器中执行此操作,因此我认为 javascript 是最佳选择。
  • 用户使用 IE 作为主要浏览器,但我不能要求他们更改安全设置(vbscript/activxcontrol 不起作用)

【问题讨论】:

  • “从网络服务器下载巨大的多部分 csv 文件并在为用户打开之前在客户端加入这些部分”几乎是不这样做的原因。如果用户必须下载庞大的数据集,那么您的解决方案就不是正确的解决方案。让客户端只向服务器询问用户实际可以看到的位,然后编写代码来处理这个问题,而不是在一个巨大的过程中需要所有数据。
  • 我完全同意你的看法。但是应用程序已经存在。今天用户手动合并文件。我只是想自动化这部分。

标签: javascript download multipart filemerge


【解决方案1】:

如果你别无选择,只能使用这个,你可以使用聚合:

var data; // will contain the retrieved data

/**
 * Grab data from the first URL in the list, adding it
 * to the global data variable. If the list is empty,
 * we got all the data and we succeed. If an XHR fails, we fail.
 */
function prepData(urlList, success, fail) {
  if(urlList.length===0) {
    succes(); // wrap in timeout if you need to break out of call chain
    return;
  }
  var xhr = new XMLHttpRequest();
  var url = urlList.splice(0,1)[0];
  xhr.open("GET", url, true);
  xhr.onreadystatechange = function() {
    if (xhr.status === 200 && xhr.readyState === 4) {
      data += xhr.responseText;
      prepData(urlList, success, fail);
    } else {
      fail(urlList, xhr);  // again, wrap in timeout to break out of call chain
    }
  }
}

然后我们可以使用如下代码调用它:

/**
 * prepare our data prior to application "start"
 */
prepData(['...','...','...'], function (){
  console.log("finished aggregating data");
  // all went well. Start the actual data-consuming code
}, function(notLoadedList, failedXhrObject){
  console.log("ohnoes!", notLoadedList, failedXhrObject);
  // something went wrong, and we can fail gracefully
});

【讨论】:

    猜你喜欢
    • 2021-03-10
    • 2012-11-22
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 2018-07-06
    • 2013-12-04
    • 2010-11-15
    • 1970-01-01
    相关资源
    最近更新 更多