By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,101 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,101 IT Pros & Developers. It's quick & easy.

cannot use webclient upload when filename is programatically retrieved

P: 1
I am trying to write a windows utility that downloads some files from an ftp site to a local directory and then uploads those files to another ftp site. The files that I need to download are posted on the ftp site everyday. The file name contains the datetime stamp, so the filename changes everyday. Files are named as such: abc.com.ftc1.062210083741

I am using a ftpwebrequest method to get a listing of all the files on the ftp directory. Then I use regex to extract the name of the file I need to download (matching on abc.com.ftc1.). After I have this file name I know exactly what file I need to download, so I use webclient DownloadData method to download the file I need to download.

After that I use webclient upload method to upload the same file to the second ftp site.

The problem I have is that webclient upload method throws an exception when I pass the variable that holds filepath to the upload method. If I assign the filepath to the variable manually from within code, then the upload works. Remember, I cannot hardcode the file name from within code as the filename changes everyday.

Let me paste some code and things will be more clear.

First I get a listing of all the file names on the ftp directory in a stream reader. I assign the contents of the stream reader to a string variable called myFiles. This part of the code works great.

//Gets names of all files on the ftp site
Expand|Select|Wrap|Line Numbers
  1.  private void ListDirectory(string serverFolderURL, string ftpUser, string ftpPass)
  2.         {
  3.             NetworkCredential creds = new NetworkCredential(ftpUser, ftpPass);
  4.             StreamReader strmReader = null;
  5.             try
  6.             {
  7.                 FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(serverFolderURL);
  8.                 listRequest.Credentials = creds;
  9.                 listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  10.                 FtpWebResponse serverResponse = (FtpWebResponse)listRequest.GetResponse();
  11.                 strmReader = new StreamReader(serverResponse.GetResponseStream());
  12.                 myFiles = strmReader.ReadToEnd();
  13.             }
  14.             catch (System.Exception ex)
  15.             {
  16.                 messageString += ex.Message.ToString();
  17.             }
  18.  
  19.             if (strmReader != null)
  20.                 strmReader.Close();
  21.         }  


Since myFiles contains a string that has all the file names on the ftp site, I use regex to extract the name of the file that I need to download.


//Finds the ftc1 filename and sets it to the ftc1Filename variable
Expand|Select|Wrap|Line Numbers
  1.         private void SetFtc1Filename(string myString)
  2.         {
  3.             string pattern = @"\abc.com.ftc1.\w*\b";
  4.             Match myMatch = Regex.Match(myString, pattern);
  5.             if (myMatch.Success)
  6.             {
  7.                 ftc1Filename = myMatch.Value.ToString().Trim();
  8.             }
  9.         } 

Now the variable ftc1Filename contains the name of the file I need to download. I use webclient download method to download the file I need to a local directory. This part of the code works great.

Expand|Select|Wrap|Line Numbers
  1.  private void DownloadFile(string fileName)
  2.         {
  3.             WebClient request = new WebClient();
  4.             request.Credentials = new NetworkCredential(downloadFtpUser, downloadFtpPass);
  5.             try
  6.             {
  7.                 byte[] fileData = request.DownloadData(downloadFtpAddress + "\\" + fileName);
  8.                 FileStream file = File.Create(localDownloadLoc + fileName);
  9.                 file.Write(fileData, 0, fileData.Length);
  10.                 file.Close();
  11.             }
  12.             catch (System.Exception ex)
  13.             {
  14.                 messageString += ("\r\n" + ex.Message.ToString());
  15.             }
  16.         } 


Now that I've downloaded the file I need, I need to upload it to the destination ftp site. I use web client upload method to do this.

Expand|Select|Wrap|Line Numbers
  1.  private void Upload(string uploadFtpAddress, string uploadFtpUser, string uploadFtpPass, string filename)
  2.         {
  3.                 using (System.Net.WebClient client = new System.Net.WebClient())
  4.                 {
  5.                     client.Credentials = new System.Net.NetworkCredential(uploadFtpUser, uploadFtpPass);
  6.                     client.UploadFile(uploadFtpAddress + "/" + new FileInfo(filename).Name, "STOR", filename);
  7.  
  8.                 }
  9.         } 


The above method is the one that throws an exception: The remote server returned an error: (500) Syntax error, command unrecognized.

I am calling the webclient upload method with the following statements:

Expand|Select|Wrap|Line Numbers
  1.  ftc1FilePath = localDownloadLoc + ftc1Filename;
  2.  
  3. if (System.IO.File.Exists(ftc1FilePath))
  4.             {
  5.                 try
  6.                 {
  7.                     Upload(uploadFtpAddress, uploadFtpUser, uploadFtpPass, ftc1FilePath);
  8.                     messageString += ("\r\n" + ftc1Filename + " " + "was successfully uploaded to" + " " + uploadFtpAddress);
  9.                 }
  10.                 catch (System.Exception ex)
  11.                 {
  12.                     messageString += ("\r\n" + ftc1Filename + " " + "could not be uploaded to" + " " + uploadFtpAddress + "." + " " + ex.Message.ToString());
  13.                 }
  14.             } 


The variable localDownloadLoc has been initialized with @"C:\FTC Move Files\" and ftc1FilePath already contains the name of the file (retrieved using regex).

Therefore, "ftc1FilePath = localDownloadLoc + ftc1Filename;" variable has to compete path for the file on the local directory that is to the uploaded.

Now here's the funny thing. If I replace ftc1Filename in the above statement with the actual name of the file, then the webclient upload method works, like this

ftc1FilePath = localDownloadLoc + "abc.com.ftc1.062210083741"; //This works

Stepping through the code, I can see that both "localDownloadLoc + "abc.com.ftc1.062210083741"" and "localDownloadLoc + ftc1Filename" contain exactly the same value, but the former works and the latter does not.

Any help with this will be highly appreciated. Sorry the post turned out to be so long, but I tried to provide as much info as possible.

Thanks.

RJ
Jul 1 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.