C# 在C Sharp中通过FTP上传PDF文件
我有以下代码通过ftp上传pdf文件:C# 在C Sharp中通过FTP上传PDF文件,c#,winforms,pdf,ftp,C#,Winforms,Pdf,Ftp,我有以下代码通过ftp上传pdf文件: try { if (!File.Exists(localPath)) throw new FileNotFoundException(localPath); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ftpPath)); request.Method
try
{
if (!File.Exists(localPath))
throw new FileNotFoundException(localPath);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ftpPath));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Proxy = null;
request.UseBinary = true;
request.Credentials = new NetworkCredential(username, password);
byte[] data = File.ReadAllBytes(localPath);
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
if (response == null || (response.StatusCode != FtpStatusCode.CommandOK))
throw new Exception("Upload failed.");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
throw e;
}
我的问题是它只上传没有图像的文本。我怎样才能上传一个文件而不阅读它?我的意思是,我只想选择并重命名该文件并将其上载。使用WebClient类,然后使用UploadFile方法。 从
String uriString = Console.ReadLine();
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
myWebClient.Credentials=new NetworkCredential(username, password);
Console.WriteLine("\nPlease enter the fully qualified path of the file to be uploaded to the URI");
string fileName = Console.ReadLine();
Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString);
// Upload the file to the URI.
// The 'UploadFile(uriString,fileName)' method implicitly uses HTTP POST method.
byte[] responseArray = myWebClient.UploadFile(uriString,fileName);