开发者

How to upload a file in a non-default directory using FTPWEBREQUEST?

I face an issue while I try to upload a file in a non-default directory of the server.

When I use "ftp://server01/autofile/test.zip", the file gets uploaded without any issues since autofile is the default directory.

But when I use the below code, I get an exception which says "The remote server returned an error: (550) File unavailable (e.g., file not found, no access).". Shown below is the piece of code I use.

string inputfilepath = "E:\\Test\\test.ZIP";
string ftpfullpath = "ftp://server01/../bcp/ftp/ftpsftiu/test.ZIP";
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Method = WebRequestMethods.Ftp.UploadFile;
ftp.Credentials = new NetworkCredential("Username", "password");
ftp.UsePassive = true;
ftp.KeepAlive = true;
ftp.UseBinary = true;
FileStream fs = File.OpenRead(inputfilepath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream reqStream = ftp.GetRequestStream();

But I can open the above non-default directory path(ftp://server01/../bcp/ftp/ftpsftiu/test.ZIP) through the Run Command of windows.

How do I upload my file to this non-default directory of the server through C# code? please开发者_Go百科 help me to fix this.

Thanks in advance!!!

With regards, Shadu


It worked for me if I put another slash after the server (//server//). I did (also) fully qualify the output path name. The static GetCread() is a method call into a home-grown class.

Example:

static void Main(string[] args)
  {
     string strSystem = "MAPS";
     FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + strSystem +"//export/home/hinest/science/trans/fred.txt");
     req.Credentials = CGetCred.GetCred(strSystem);
     req.Method = WebRequestMethods.Ftp.UploadFile;
     FtpWebResponse resp = (FtpWebResponse)req.GetResponse();
     StreamWriter fileOut = new StreamWriter(req.GetRequestStream());
     StreamReader fileIn = new StreamReader(@"c:\science\"+strSystem+".txt");
     while (!fileIn.EndOfStream)
     {
        fileOut.WriteLine(fileIn.ReadLine());
     }
     fileIn.Close();
     fileOut.Close();
     resp.Close();
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜