1、从ftp下载pdf的方法
public static void DownloadPdfFileFromFtp(string ftpUrl,string user,string password string localPath)
{
// 创建FtpWebRequest对象
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(username, password);
// 使用WebResponse获取响应
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
// 获取文件流
Stream responseStream = response.GetResponseStream();
// 创建文件流写入器,将数据写入本地文件
using (FileStream fileStream = new FileStream(localPath, FileMode.Create))
{
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) != 0)
{
fileStream.Write(buffer, 0, bytesRead);
}
}
}
}
2、创建指定路径的文件夹
string tempPath = "C:\\ftpfile\\";
//用DirectoryInfo拼接路径
DirectoryInfo di = new DirectoryInfo(string.Format(@"{0}\{1}", tempPath, "图纸PDF"));
if (!di.Exists)
{
di.Create();
}
string filepdf = di.FullName + "\\" + "图纸_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
3、调用
string user="用户名";
string password="密码";
DownloadPdfFileFromFtp(ftpServerUrl, user,password,filepdf);