15

I have an webDav CORS plugin, which I can use to POST/PUT/GET/REMOVE/ALLDOCS files on a webDav server.

I now want to do the same for FTP, but I'm struggling to get the xmlhttprequest-syntax to work (I'm just getting error 0 ).

This page on Mozilla says it's possible to use xmlhttprequests for file and ftp as well, but I cannot find a working example or tutorial anywhere.

This is what I'm trying, which returns access to restricted URI denied

function reqListener () {
  console.log(this.responseText);
}

var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.open("GET", "ftp://<username>:<passeword>@mydomain.de/folder/test.txt", true);
oReq.send();

I also tried a regular Ajax request

$.ajax({
  url: "ftp://sharedspace.domain.provider.com/folder/test.txt",
  type: "GET",
  async: true,
  dataType: "text",
  crossdomain : true,
  headers : {
    user: "<username>",
    password: "<password>"
  },
  success: function(e){
    console.log("success");
    console.log(e);
  },
  error: function(e){
    console.log("error");
    console.log(e);
  },
}); 

which also does not work, returning 0 status code.

Question:
What is the correct syntax to do a cross-domain XMLHTTPREQUEST for FTP.

Thanks!

EDIT:
The only useful link I found is this page here, but it's just bits and pieces of information and I couldn't puzzle them together.

EDIT
Maybe also useful link

frequent
  • 27,643
  • 59
  • 181
  • 333

2 Answers2

19

Although the Mozilla MDN docs reference xmlHttpRequest supporting file and ftp none of the major browsers do AFAIK. It is one of the reasons why you need to serve your web projects from some sort of server, even if it is on the same machine, if you want to develop/test any xmlHttpRequest stuff since file:// doesn't work.

Microsoft specifically states that IE only supports http/https. The W3C spec for it also says that the spec is only for HTTP/HTTPS but that 'some implementations support protocols in addition to HTTP and HTTPS, but that functionality is not covered by this specification'.

As for CORS, it is specifically only for HTTP/HTTPS. The spec is all about using HTTP headers. See the W3C spec here. FTP doesn't have any equivalent type of header as HTTP.

pseudosavant
  • 7,056
  • 2
  • 36
  • 41
  • ok. thanks for the info. I posted the best link I could find on how to get it done, so still keeping fingers crossed it's possible. – frequent Feb 27 '13 at 12:50
  • @frequent Good luck. Pretty sure it isn't possible without some intermediary HTTP server proxying the FTP requests. If it works, it looks like it will only be in Firefox. – pseudosavant Feb 27 '13 at 21:39
  • @frequent I guess I didn't get the bounty? – pseudosavant Mar 05 '13 at 23:24
  • just did. Not the answer I was hoping for tough... :-) – frequent Mar 06 '13 at 08:31
  • @frequent Thanks. Are you running server-side code for this project? You could implement an ajax to FTP proxy. There is probably already an open source FTP client library for the server-side language you are using. – pseudosavant Mar 07 '13 at 01:03
  • I'm doing both client and server-side so would be no problem. I was thinking of doing a client-side only application with the server just maintaining big JSON-files. My client would be able to query an index I supply to retrieve Ids and then the idea was to range-request the ids - probably to far out of the picture tough. I will have a look at FTP proxy (matter of fact - [here](http://stackoverflow.com/questions/14839838/what-is-the-syntax-to-do-a-cross-domain-xmlhttprequest-to-an-ftp-server)) :-) – frequent Mar 07 '13 at 08:01
1
oReq.open("PUT", "ftp://`<username`>:`<password`>@mydomain.de/folder/test.txt", true);

req.setRequestHeader('Content-Type', "text/plain");

req.send("Content of test.txt. This will be in test.txt");
Remees M Syde
  • 2,564
  • 1
  • 19
  • 42
Stepo
  • 1,036
  • 1
  • 13
  • 24