C# C Ftp上传二进制文件不会上传
试图找到一个文件并将其上传到ftp服务器,我想我一切正常,但它没有上传任何东西C# C Ftp上传二进制文件不会上传,c#,file-upload,ftp,C#,File Upload,Ftp,试图找到一个文件并将其上传到ftp服务器,我想我一切正常,但它没有上传任何东西 string filepath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); DirectoryInfo d = new DirectoryInfo(filepath); List<String> allDatfiles = Directory
string filepath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
DirectoryInfo d = new DirectoryInfo(filepath);
List<String> allDatfiles = Directory
.GetFiles(filepath, "data.dat", SearchOption.AllDirectories).ToList();
foreach (string file in allDatfiles)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.test.com/Holder");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("User", "Pass");
request.UseBinary = true;
request.UsePassive = true;
byte[] data = File.ReadAllBytes(file); // Think the problem is with file
request.ContentLength = data.Length;
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
}
还尝试将文件位置作为带有@C的字符串放入。。。
我没有收到任何错误,上传后也没有显示任何文件
您是否检查了服务器上的用户权限。。用户是否可以写入
目录
如果您使用linux服务器
您是否尝试过在调试器中运行代码,并在其运行时检查变量内容?我们无法从这里看到它们,也不知道文件是否在您要查找的位置。您必须自己调试,以查看失败的地方。是的,文件位于该位置,甚至使用具有特定名称的文本文件进行了尝试,但没有任何效果调试器不会抛出错误我没有说任何关于抛出错误的内容。请读我写的。我们无法单步查看代码,因为我们无法看到它在运行时做了什么。只有你可以这样做,因为只有你有权这样做。
private static void UploadFile(string dir, Uri target, string fileName, string username, string password, string finilizingDir, string startupPath, string logFileDirectoryName)
{
try
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
request.Proxy = null;
request.Method = WebRequestMethods.Ftp.UploadFile;
// logon.
request.Credentials = new NetworkCredential(username, password);
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(dir + "\\" + fileName);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
if (response.StatusCode == FtpStatusCode.ClosingData)
{
Console.WriteLine(" --> Status Code is :" + response.StatusCode);
}
Console.WriteLine(" --> Upload File Complete With Status Description :" + response.StatusDescription);
response.Close();
}
catch (Exception ex)
{
Console.WriteLine("*** Error Occurred while uploading file :" + fileName + " System Says :" + ex.Message + " **********END OF ERROR**********");
}
}