2013-02-23 119 views
0

我需要一些帮助以满足以下要求。在javascript中下载和合并多部分文件

  • 从web服务器下载巨大的多部分csv文件,然后在打开用户之前在客户端加入这些部分。
  • 我可以在下载之前获取零件的数量并从网络服务器获取它们的URL。
  • 我需要在客户端浏览器中做到这一点,因此我认为JavaScript是最好的选择。
  • 用户使用IE作为主要浏览器,但我不能要求他们改变安全设置(VBScript中/ activxcontrol将无法正常工作)
+0

“从网络服务器下载庞大的多csv文件,并打开它之前加入在客户端的部分用户“几乎是不这样做的原因。如果用户必须下载一个庞大的数据集,那么您的解决方案并不是正确的解决方案。让客户端向服务器询问用户实际可以看到的位,然后编写代码来处理这些问题,而不是需要一个巨大的数据。 – 2013-02-23 16:34:26

+0

我完全同意你的看法。但应用程序已经在那里。今天用户手动合并文件。我只是试图自动化该部分。 – user2102619 2013-02-23 18:11:01

回答

0

如果你没有选择,只能与这个工作,你可以使用一个集合:

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 
});