Avatar of Joseph Longo
Joseph Longo
Flag 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

C#

Avatar of undefined
Last Comment
Joseph Longo

8/22/2022 - Mon
Vishal Tankariya

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
ktaczala

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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>
Joseph Longo

ASKER
just to be clear this is a file upload
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
SOLUTION
Vishal Tankariya

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Joseph Longo

ASKER
this is working.
i needed the full file name