Link to home
Start Free TrialLog in
Avatar of Joseph Longo
Joseph LongoFlag for United States of America

asked on

c# FTP ftpwebrequest URI invalid

i think the file name may be incorrect but i cannot tell why this throwing this exception.
any ideas?

fileName = "c:/soapmessages/OUT/" + var_yr + var_mth + var_day + "_XXXXXXX.csv";

error:
System.Net.WebException: The requested URI is invalid for this FTP command.
   at System.Net.FtpWebRequest.GetRequestStream()
   at XXXXXXXX.Program.Main(String[] args) in J:\mastersource\XXXXXX\ConsoleApplication2\Program.cs:line 179



string ftpfullpath = Path.GetFileName(ftpFile);
              FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpFile));
              ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

              ftp.KeepAlive = true;
              ftp.UseBinary = true;
              ftp.Method = WebRequestMethods.Ftp.UploadFile;

              FileStream fs = File.OpenRead(fileName);
              byte[] buffer = new byte[fs.Length];
              fs.Read(buffer, 0, buffer.Length);
              fs.Close();

             [b] Stream ftpstream = ftp.GetRequestStream();      <--- line 179[/b]
              ftpstream.Write(buffer, 0, buffer.Length);
              ftpstream.Close();

Open in new window

Avatar of Vishal Tankariya
Vishal Tankariya
Flag of India image

Hi,

You passed wrong URI just pass ftp uri to get actual file.

for e.g.
yourfolderpath = Which is display in filezilla.
use: "ftp://httpdocs/yourfilename" or use "ftp://youerwebsitename/httpdocs/yourfilename"

FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("ftp://yourfolderpath");

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ktaczala
ktaczala
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Joseph Longo

ASKER

ok so its transferring an empty file.

i think the filestream isn't working.  



<code>
             string ftpfullpath = Path.GetFileName(ftpFile);
              FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpFile));
              ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

              ftp.KeepAlive = true;
              ftp.UseBinary = true;
              ftp.Method = WebRequestMethods.Ftp.UploadFile;

              FileStream fs = File.OpenRead(fileName);
              byte[] buffer = new byte[fs.Length];
              fs.Read(buffer, 0, buffer.Length);
              fs.Close();

              Stream ftpstream = ftp.GetRequestStream();
              ftpstream.Write(buffer, 0, buffer.Length);
              ftpstream.Close();

</code>
just to be clear this is a file upload
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this is working.
i needed the full file name