FTP下载文件返回文件路径和返回byte[]方法,以PDF为例
FTP下载文件保存到本地并返回文件路径
public static string returnFilePath(string filePath, int yymxid)
{
FtpWebRequest reqFTP;
try
{
string newFilePath = filePath.Replace("", "");
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(newFilePath));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
string returnPath = @"D:\" + yymxid .ToString()+ ".PDF";
if (File.Exists(returnPath))
{
//如果存在则删除
File.Delete(returnPath);
}
Stream responseStream = response.GetResponseStream();
FileStream filestream = new FileStream(returnPath, FileMode.Create, FileAccess.Write);
int buflength = 8196;
byte[] buffer = new byte[buflength];
int bytesRead = 1;
while (bytesRead != 0)
{
bytesRead = responseStream.Read(buffer, 0, buflength);
filestream.Write(buffer, 0, bytesRead);
}
filestream.Close();
return returnPath;
}
catch (Exception)
{
return "";
}
}
C#下载文件返回byte[]
public static byte[] DownLoad(string filePath)
{
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(filePath));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
var response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
var ms = new MemoryStream();
byte[] buffer = new byte[1024];
while (true)
{
int sz = ftpStream.Read(buffer, 0, 1024);
if (sz == 0) break;
ms.Write(buffer, 0, sz);
}
ftpStream.Close();
ms.Position = 0;
var bytes = new byte[(int)ms.Length];
ms.Read(bytes, 0, bytes.Length);
ms.Close();
return bytes;
}
catch (Exception err)
{
return null;
}
}
mvc中输出 byte[]
public FileResult AkbgDetail()
{
string filePath = Common.FtpHelper.returnFilePath(GetFilePath,yymxid);
FileStream pFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
byte[] pReadByte = new byte[0];
BinaryReader r = new BinaryReader(pFileStream);
r.BaseStream.Seek(0, SeekOrigin.Begin); //将文件指针设置到文件开始
pReadByte = r.ReadBytes((int)r.BaseStream.Length);
var fileContent = File(pReadByte, HttpUtility.UrlEncode("报告.pdf", System.Text.Encoding.UTF8));
pFileStream.Close();
if (System.IO.File.Exists(filePath))
{
//如果存在则删除
System.IO.File.Delete(filePath);
}
string userClient = Request.UserAgent;
if (userClient.ToLower().Contains("iphone") || userClient.ToLower().Contains("ipad") || userClient.Contains("windows nt") || (userClient.Contains("windows nt") && userClient.Contains("compatible; msie 9.0;")))
{
return fileContent;
}
else
{
return File(pReadByte, "application/pdf", "报告.pdf");
}
}
WEBFORM中输出byte[]:
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "inline; filename=" + HttpUtility.UrlEncode("demo.PDF", System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.Clear();
Response.Close();