473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

cannot use webclient upload when filename is programatically retrieved

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
0 1358

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

Similar topics

6
by: MrDom | last post by:
I am wondering if someone can help solve this question I have a table in sql server 2000, I setup it using Enterprise manager. When I generate an SQL Script for this table it scripts as: ...
4
by: Diarmaid | last post by:
Hi! I've built a dynamic navigation system which compares the filename in the document URL with the filename in the navigation link in order to determine which link to highlight as "active". ...
0
by: Jérôme Piquot | last post by:
Hi, When i want to save configuration settings i have a "ConfigurationSection properties cannot be edited when locked" Exception. StackTrace " at...
0
by: TonyR | last post by:
Can anyone help? I am writing an application to upload a data file to a remote website running IIS and FP2002 extensions. If I use "My Network Places" in windows XP, I can do this with...
16
by: lawrence k | last post by:
I've a file upload script on my site. I just now used it to upload a small text document (10k). Everything worked fine. Then I tried to upload a 5.3 meg Quicktime video. Didn't work. I've...
7
by: pedagani | last post by:
Dear comp.lang.c++, I'm trying to read a file with very long filename using ifstream. Although, the file exists the file open for read fails. Is there a restriction on the size? I'm using winXP...
3
by: =?Utf-8?B?dmFzaW1v?= | last post by:
Have the following code: wkLocalPath = "C:\A\1.jpg" wkServerPath = "http://servername/dirname/1.jpg" wcClient.Credentials = New NetworkCredential("UID", "PW") wcClient.UploadFile(wkServerPath,...
1
by: JahMic | last post by:
Can someone enlighten me to what's going on here. I get the following warning: Warning: curl_setopt() : CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set...
2
by: Mitch | last post by:
Not sure where to post this but simply I want to upload a file using the WebClient, ie: Try Dim wc As New WebClient wc.Credentials = New System.Net.NetworkCredential("someuser", "somepass")...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.