通过使用C#上传文件我遇到了问题: 当我试图在其路径和/或文件名中上传包含德语变音符号(ä,ö,ü)的文件时,我得到一个FTP错误553“文件名不允许”。
我可以上传没有变音符号的文件而没有任何问题。我检查过,如果编码是正确的,我的FTP上启用了utf-8。我真的不想改变任何包含变音符号的文件,所以我希望我能在这里做些什么。
当我通过Filezilla上传文件时,一切都很好。下载或获取这些文件也没有问题。只上传它们会导致错误。
这很好用:
ftp://ftp.myurl.com/mainfolder/folder/filename [id].png
这会给我错误553:
ftp://ftp.myurl.com/mainfolder/folder/filenäme [id].png
ftp://ftp.myurl.com/mainfolder/földer/filename [id].png
这是我的源代码:
string fullpath = ftp.url + folder + "/" + file.name;
fullpath = fullpath.Replace("ä", "%C3%A4").Replace("ü", "%C3%BC").Replace("ö", "%C3%B6").Replace(" ","%20");
Uri uri = new Uri(fullpath);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
request.UseBinary = true;
request.KeepAlive = false;
request.UsePassive = false;
request.Timeout = 10000;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(Data.user, Data.password);
FileStream fs = new FileStream(file.fullname, FileMode.Open, FileAccess.Read);
BinaryReader sourceStream = new BinaryReader(fs, Encoding.UTF8);
Stream requestStream = request.GetRequestStream();
byte[] chunk;
chunk = sourceStream.ReadBytes(1024);
int totalLength = 0;
while (chunk.Length > 0)
{
requestStream.Write(chunk, 0, chunk.Length);
totalLength += chunk.Length;
chunk = sourceStream.ReadBytes(1024);
}
requestStream.Close();
sourceStream.Close();
request.ContentLength = totalLength;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();