Click here to Skip to main content
14,775,279 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:

I have a question regarding file downloading via FtpWebRequest. All works well when I download the needed files, but if the filename has a space in it, the filename is saved with %20 replacing the actual space (eg. remote/folder/my file.whatever becomes local\folder\my%20file.whatever. If I do a file listing of where the file is, it does not contain the encoded character in the result.

I know there is probably something silly/small that I am missing, but I'm pulling (what little is left of) my hair out!

Barring creating a helper class to find/replace the offending characters in the newly downloaded file, is there something that I can do when the file is saved that will resolve this issue?

My code is something as follows:
FtpWebRequest request = FtpWebRequest.Create(myUriWithFileName) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(username, password); // credentials as necessary...
request.UsePassive = false;//or true as is necessary
request.UseBinary = true;
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
    StreamToFile(responseStream, @"c:\Temp\my file.whatever"); // StreamToFile is just a helper to write from one stream to another...

Thanks in advance!
Wombaticus 26-Nov-15 4:52am
%20 is the hex value for the space character - it is a good thing to have it there instead of a space, especially if you are going to create links or URL's with it.
nortee 26-Nov-15 5:13am

I am not creating links/URLs and I know that that is the hex value for the space character. My issue is that I am downloading a file which has a space in it's name and when I do a file listing of the folder it is returned correctly (without the encoded/hex value), but when it saves the file with that file name it replaces it with the %20.

My question was how to mitigate this file name change without resorting to a helper method to change the file name back to spaces (after it has saved onto my local system).
Wombaticus 26-Nov-15 5:21am
Well, ok, but I don't think you need to create a helper method - can't you just use Server.UrlDecode to do it?
nortee 26-Nov-15 6:22am
Thanks for your response,

I should have mentioned (apologies), that this is a windows service and/or desktop application and not a web-based application.
Wombaticus 26-Nov-15 6:35am
You can still use the WebUtility class for this
nortee 26-Nov-15 6:56am
Thanks Wombaticus,

I will have a look see at that one and advise.
nortee 27-Nov-15 7:56am
Hi Wombaticus,

Thanks for your suggestions. Instead of fighting with all that stuff, I ended up using a third party component that did what I needed it to do.
Thanks once again for your input. It is/was greatly appreciated!
Richard Deeming 27-Nov-15 8:42am
The problem is most likely within your StreamToFile method, which you haven't shown.

1 solution

Thanks one and all, as noted above I ended up using a third party component to do what I needed it to do.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900