RejectedSoftware Forums

Sign up

Uploading a file to ftp server?

How do I upload a file to ftp server?

I checked the API but there is no upload on vibe.inet.urltransfer only download... is there a way that we could add this?

for now I will try to use the std.net.curl...

Re: Uploading a file to ftp server?

On Sat, 29 Aug 2015 10:24:45 GMT, Louie Bacani Foronda wrote:

How do I upload a file to ftp server?

I checked the API but there is no upload on vibe.inet.urltransfer only download... is there a way that we could add this?

for now I will try to use the std.net.curl...

Hello, here is how i put it,

add libs: ["curl"], to dub.json

import std.net.curl;

auto file = normalize("your/filename.ext");
auto ftp = FTP();
ftp.setAuthentication("username","password"); // for those who have userpwd
upload(file,'ftp://ftpsite/folder/filename.ext', ftp);

take note you must specify the whole filename.ext on the second param or you will get bad illegal url on handle error...

Re: Uploading a file to ftp server?

On Sat, 29 Aug 2015 16:49:40 GMT, Louie Bacani Foronda wrote:

On Sat, 29 Aug 2015 10:24:45 GMT, Louie Bacani Foronda wrote:

How do I upload a file to ftp server?

I checked the API but there is no upload on vibe.inet.urltransfer only download... is there a way that we could add this?

for now I will try to use the std.net.curl...

Hello, here is how i put it,

add libs: ["curl"], to dub.json

import std.net.curl;

auto file = normalize("your/filename.ext");
auto ftp = FTP();
ftp.setAuthentication("username","password"); // for those who have userpwd
upload(file,'ftp://ftpsite/folder/filename.ext', ftp);

take note you must specify the whole filename.ext on the second param or you will get bad illegal url on handle error...

I'd recommend to execute that block of cURL code in a separate thread (easiest by using runWorkerTask({ ... });), because it will otherwise block the main thread's event loop during the upload, so that no other requests can be served (curl uses synchronous I/O internally).

Alternatively, since FTP is a simple protocol, it might make sense to implement the upload manually (untested and missing error checking, but this is roughly how it should work):

struct FTPStatus {
    int code;
    string message;
}

FTPStatus readStatus(TCPConnection conn)
{
    auto ln = conn.readLine();
    FTPStatus ret;
    ret.code = ln.parse!int;
    do {
        if (ln.startsWith("-")) message ~= ln[2 .. $] ~ "\n";
        else message ~= ln[1 .. $];
    } while (ln.startsWith("-"));
    return ret;
}

NetworkAddress parseAddress(string status)
{
    // status is in the form "Entering passive mode (1,2,3,4,5,6)"
    auto digits = status
        .findSplitAfter("(")[1]
        .findSplitBefore(")")[0]
        .splitter(",")
        .map(str => str.to!int)
        .array;

    NetworkAddress ret;
    ret.family = AF_INET;
    ret.sockAddrInet4.sin_addr.s[0 .. 4] = digits[0 .. 4];
    ret.port = digits[4] * 1024 + digits[5];
    return ret;
}

// connect and authenticate
auto conn = connectTCP("ftpsite", 21);
enforce(readStatus(conn).code == 220);
conn.write("USER "~username~"\r\n");
enforce(readStatus(conn).code == 331);
conn.write("PASS "~password~"\r\n");
enforce(readStatus(conn).code == 230);

// enter passive mode
conn.write("PASV\r\n");
auto pst = readStatus(conn);
enforce (pst.code == 227);
auto addr = parseAddress(pst.message);

// upload the file
conn.write("STOR /folder/filename.ext\r\n");
auto dconn = connectTCP(addr.ip, addr.port);
auto file = openFile("your/filename.ext");
dconn.write(file);
dconn.close();

conn.write("QUIT\r\n");
conn.close();

I'll open a ticket to implement simple FTP upload/download in vibe.d.

Re: Uploading a file to ftp server?

On Wed, 09 Sep 2015 06:03:21 GMT, Sönke Ludwig wrote:

I'll open a ticket to implement simple FTP upload/download in vibe.d.

https://github.com/rejectedsoftware/vibe.d/issues/1253