VS2017 使用 Microsoft.AspNetCore.Http; 却找不到Microsoft.AspNetCore.Http命名空间,只能用NuGet安装,安装需要选用 .NETFramework,Version=v4.6.1 ,否则会出错。
因为用到 IFormFile ,需要引入两个 dll即可。
Microsoft.AspNetCore.Http
Microsoft.AspNetCore.Http.Features
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Microsoft.AspNetCore.Http;
namespace MyFtpTools
{
public class FtpHelper
{
#region FTP服务器身份验证信息
//private static string ftpServer = GlobalSwitch.ServerAddress;
//private static string username = GlobalSwitch.LoginName;
//private static string password = GlobalSwitch.LoginPWD;
private static string ftpServer;
private static string username;
private static string password;
public static string FtpServer { get => ftpServer; set => ftpServer = value; }
public static string Username { get => username; set => username = value; }
public static string Password { get => password; set => password = value; }
#endregion
#region 上传文件
///
/// 上传文件
///
///
///
public static void FileUpLoad(IFormFile file, string dir)
{
try
{
//string url = Path.Combine(ftpServer, dir);
string url = String.Format("ftp://{0}{1}", ftpServer, dir);
string fileName = file.FileName;
Uri uri = new Uri(Path.Combine(url, fileName));
using (Stream fs = file.OpenReadStream())
{
long length = fs.Length;
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
reqFTP.Credentials = new NetworkCredential(username, password);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
using (Stream stream = reqFTP.GetRequestStream())
{
int BufferLength = (Int32)length;
byte[] b = new byte[BufferLength];
int i;
while ((i = fs.Read(b, 0, BufferLength)) > 0)
{
stream.Write(b, 0, i);
}
Console.WriteLine("上传文件成功");
}
}
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 文件上传
///
/// 原路径(绝对路径)包括文件名
/// 目标文件夹
public static void FileUpLoad(string filePath, string uploadPath)
{
try
{
//string url = Path.Combine(ftpServer, uploadPath);
string url = String.Format("ftp://{0}{1}", ftpServer, uploadPath);
try
{
try
{
FileInfo fileInfo = new FileInfo(filePath);
using (FileStream fs = fileInfo.OpenRead())
{
long length = fs.Length;
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url + fileInfo.Name));
reqFTP.Credentials = new NetworkCredential(username, password);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
using (Stream stream = reqFTP.GetRequestStream())
{
int BufferLength = (Int32)length;
byte[] b = new byte[BufferLength];
int i;
while ((i = fs.Read(b, 0, BufferLength)) > 0)
{
stream.Write(b, 0, i);
}
Console.WriteLine("上传文件成功");
}
}
}
catch (Exception ex)
{
Console.WriteLine("上传文件失败错误为" + ex.Message);
}
finally
{
}
}
catch (Exception ex)
{
Console.WriteLine("上传文件失败错误为" + ex.Message);
}
finally
{
}
}
catch (Exception ex)
{
Console.WriteLine("上传文件失败错误为" + ex.Message);
}
}
#endregion
#region 下载文件
///
/// 下载文件
///
///
///
///
public static void GetFile(string dir, string fileName, string localFile)
{
string filedownloadPath = Path.Combine(ftpServer, dir);
Uri uri = new Uri(Path.Combine(filedownloadPath, fileName));
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
request.Credentials = new NetworkCredential(username, password);
request.UseBinary = true;
request.UsePassive = true;
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
int bufferSize = 65536;
using (FileStream localFileStream = new FileStream(localFile, FileMode.OpenOrCreate))
{
byte[] byteBuffer = new byte[bufferSize];
int bytesRead = stream.Read(byteBuffer, 0, bufferSize);
try
{
while (bytesRead > 0)
{
localFileStream.Write(byteBuffer, 0, bytesRead);
bytesRead = stream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
response.Close();
request = null;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 获取文件 byte[]
///
///
///
///
public static byte[] GetFileBytes(string dir, string fileName)
{
int bufferSize = 65536;
byte[] byteBuffer = new byte[bufferSize];
string filedownloadPath = Path.Combine(ftpServer, dir);
Uri uri = new Uri(Path.Combine(filedownloadPath, fileName));
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
request.Credentials = new NetworkCredential(username, password);
request.UseBinary = true;
request.UsePassive = true;
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
stream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex)
{
throw ex;
}
return byteBuffer;
}
#endregion
#region 删除文件
///
/// 删除文件
///
/// 服务器下的相对路径 包括文件名
public static void DeleteFileName(string fileName)
{
try
{
string ftpIP = ftpServer.Substring(6);
FileInfo fileInf = new FileInfo(Path.Combine(ftpIP, fileName));
string uri = Path.Combine(ftpServer, fileName);
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(username, password);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine("删除文件出错:" + ex.Message);
}
}
#endregion
#region 获取文件列表
///
/// 从ftp服务器上获得文件列表
///
/// 服务器下的相对路径
///
public static List GetFile(string RequedstPath)
{
List strs = new List();
try
{
string uri = Path.Combine(ftpServer, RequedstPath);
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(username, password);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
if (!line.Contains(""))
{
string msg = line.Substring(39).Trim();
strs.Add(msg);
}
line = reader.ReadLine();
}
reader.Close();
response.Close();
return strs;
}
catch (Exception ex)
{
Console.WriteLine("获取文件出错:" + ex.Message);
}
return strs;
}
#endregion
#region 创建文件夹
///
/// 新建目录 上一级必须先存在
///
/// 服务器下的相对路径
public static void MakeDir(string dirName)
{
try
{
string uri = Path.Combine(ftpServer, dirName);
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(username, password);
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine("创建目录出错:" + ex.Message);
}
}
#endregion
#region 删除文件夹
///
/// 删除目录 上一级必须先存在
///
/// 服务器下的相对路径
public static void DelDir(string dirName)
{
try
{
string uri = Path.Combine(ftpServer, dirName);
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(username, password);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine("删除目录出错:" + ex.Message);
}
}
#endregion
#region 获取文件夹列表
///
/// 从ftp服务器上获得文件夹列表
///
/// 服务器下的相对路径
///
public static List GetDirctory(string RequedstPath)
{
List strs = new List();
try
{
string uri = Path.Combine(ftpServer, RequedstPath);
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(username, password);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
reqFTP.KeepAlive = false;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
if (line.Contains(""))
{
string msg = line.Substring(line.LastIndexOf("") + 5).Trim();
strs.Add(msg);
}
line = reader.ReadLine();
}
reader.Close();
response.Close();
return strs;
}
catch (Exception ex)
{
Console.WriteLine("获取目录出错:" + ex.Message);
}
return strs;
}
#endregion
}
}