简体   繁体   中英

FTP - Filename not allowed

I'm trying to upload a file to an FTP server using code based on this Microsoft Article

My code looks like this for testing purposes:

string ftpUrl = "ftp://" + ftpSite + ftpPath + "test.txt";
//string ftpUrl = ftpSite;

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl);
request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
request.Method = WebRequestMethods.Ftp.UploadFile;

StreamReader srcStream = new StreamReader(filePath);
byte[] fileContents = Encoding.UTF8.GetBytes(srcStream.ReadToEnd());
request.ContentLength = fileContents.Length;

Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

Every time I try to upload the file, I get a "Filename not allowed" error back from the FTP server. If I use an FTP client application like WS_FTP, I'm able to FTP the same file just fine.

Any thoughts on how to correct this? I've already tried setting active/passive FTP mode, keepalive, and binary modes without any luck.

EDIT

This is a winforms application - the filename comes in from an OpenFileDialog prompt and the FTP address is based on settings in App.Config.

Without seeing your full code, I will say there is a very good chance the constructed FTP URL / path is incorrect, in comparison to what you expect it to be when you manually connect to the FTP site through a FTP client.

If you post your app.config code and how you assign values to ftpSite and ftpPath , it would be helpful in answering this question.

You can get that particular error for many cases.

Most common issue is that the path you are accessing is not valid by the permissions allowed, and using a relative path or changing thew path might get it fixed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM