2016-11-23 60 views
2

我正在使用jQuery ajax加载保存在FTP服务器中的文件。需要显示Progress loader中加载的文件的百分比。Ajax调用使用FTP加载Json文件,需要显示百分比加载进度条

以前我有HTTP请求和使用XMLHttpRequest工作。以下是有效的代码。

$.ajax({ 
    xhr: function() { 
     var xhr = new window.XMLHttpRequest(); 

     // Upload progress 
     xhr.upload.addEventListener("progress", function(evt){ 
     if (evt.lengthComputable) { 
      var percentComplete = (evt.loaded/evt.total)*100; 
      var loadPercent = '<div id="fountainTextG">'+Math.round(percentComplete)+'% complete ..</div>'; 
      $(".sqlLoading").html(loadPercent).removeClass("hide"); 
      jsAPP.sqlLoading = true; 
     } 
     }, false); 

     // Download progress 
     xhr.addEventListener("progress", function(evt){ 
      if (evt.lengthComputable) { 
      var percentComplete =(evt.loaded/evt.total)*100; 
      var loadPercent = '<div id="fountainTextG">'+Math.round(percentComplete)+'% complete ..</div>'; 
      $(".sqlLoading").html(loadPercent).removeClass("hide"); 
      jsAPP.sqlLoading = true; 
      } 
     }, false); 

     return xhr; 
    }, 
    type: 'POST', 
    url:'ftp://192.168.1.157/pub/1.json', 
    dataType: "jsonp", 
    jsonpCallback:"abc", 
    success: function(obj){ 
     console.log("File loaded successfully"); 

    }, 
    error:function(err,stat,erroT){ 

     $(".page").html("<div class='data_error'> Sorry! No data available for this city.</div>"); 
    } 
}); 

但是这不适用于FTP请求。有没有办法在FTP上显示进度加载器?请帮助。

+0

只是好奇 - ajax是否支持ftp? –

+0

好吧,我做了一些挖掘,并认为[XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest)支持其他协议即viz。 FTP。也许你可以使用[File API](https://developer.mozilla.org/en-US/docs/Web/API/File)与XMLHttpRequest一起来实现你的目标。 –

回答

0

我试过&测试了三种通过js访问文件的方式,这是我对这个问题的看法。

XMLHttpRequest

  • 虽然XMLHttpRequest的指出它支持其他协议,它 似乎没有访问文件通过FTP服务。
  • 当我使用下面的代码尝试访问时,我遇到了预期的 的CORS错误。
  • XMLHttpRequest cannot load ftp://ftp.funet.fi/pub/standards/RFC/rfc959.txt. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

  • FTP服务器似乎并没有起到访问控制头,也由 post

    testFtpLoad : function(){ var xhr = new XMLHttpRequest(); xhr.open("GET", "ftp://ftp.funet.fi/pub/standards/RFC/rfc959.txt", true); xhr.onload = function (e) { if (xhr.readyState === 4) { if (xhr.status === 200) { console.log(xhr.responseText); } else { console.error(xhr.statusText); } } }; xhr.onerror = function (e) { console.error(xhr.statusText); }; xhr.send(null); },

Anchor tag

如果证实您使用的是现代浏览器,您可以使用下载属性(这是一个简单的<a>标签用法)直接将ftp提供给用户,但这不是您要查找的内容。

testUsingAnchorTag : function(){ var $aTag = document.createElement("a"); $aTag.href = "ftp://ftp.funet.fi/pub/standards/RFC/rfc959.txt"; $aTag.id = "temporaryDownloadLink"; $aTag.download = "rfc959.txt"; document.body.appendChild($aTag); $aTag.click(); setTimeout(function(){ $('#temporaryDownloadLink').remove(); }, 1000); }

  • 下行:虽然这个下载用户的计算机上的文件,你 将无法​​跟踪它的进展

File API

  • 我尝试使用ftp url访问文件,但我t抱怨了我通过的参数 。

    var f = new File("ftp://ftp.funet.fi/pub/standards/RFC/rfc959.txt", true)

  • 即使我要成功地传递正确的一组则params的, 它会抱怨URL的性质,因为它只有 预计到服务器/从用户的计算机提供服务的路径提到 这post - 纠正我,如果我在这里错了。

Concl:

  • 最后我想得出结论,这是不可能的服务& 通过FTP从浏览器中使用JS
  • 跟踪文件的进度您可能必须回退到您的HTTP协议并通过HTTP通过服务器提供文件以实现您的目标

我已经链接到我翻遍的大部分资源 - 这里还有更多。

希望这有助于。