Search code examples
c#ftpibm-midrangeftpwebrequest

File Upload to AS400 using FtpWebRequest


I’m attempting to upload a file to AS400 using C#. The structure is:

ftp_server_ip_address/library/table

E.g.:

10.123.1.23/ABCD/XYZ

FileName : sample.txt

The AS400 table has same fields as my file layout. I am able to successfully upload file using FTP commands using PUT command as below.

ftp> open 10.123.1.23
ftp> username
ftp> password
ftp> cd ABCD
ftp> put c:/sample.txt ABCD/XYZ

I get a success message.

But when I try using FtpWebRequest, I get a 550 :” File unavailable or no access” error message.

I’m not sure how to construct the URI. Currently, my code looks like

FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(@"ftp://110.123.1.23/ABCD” + file.Name);

Also, I'm unable to browse to the FTP folder using Internet Explorer or Windows Explorer. I get the same 550 error. Any help is much appreciated.


Solution

  • You're constructing the URL a bit wrong, you're missing a / before the file name, should probably be like this;

    FtpWebRequest ftpRequest = 
        (FtpWebRequest)WebRequest.Create(@"ftp://110.123.1.23/ABCD/” + file.Name);
    

    You're also not storing in the same directory as your interactive example;

    ftp> cd ABCD
    ftp> put c:/sample.txt ABCD/XYZ
    

    will store the file XYZ in the directory ABCD/ABCD, not just ABCD as in your URL example.

    Thirdly, if you're using .NET 4.0 or higher, you may be bitten by this changed behavior in how directories are sent to the server.