C#導(dǎo)出pdf的實(shí)現(xiàn)方法(瀏覽器不預(yù)覽直接下載)
前言
這篇文章主要給大家介紹了關(guān)于C#導(dǎo)出pdf的實(shí)現(xiàn)方法,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧
方法如下:
一.接口部分的代碼
[HttpGet]
public HttpResponseMessage ExportPdf(string id)
{
string pdfName = "";
//id 查詢(xún)條件,根據(jù)實(shí)際情況修改即可
//pdfName 例如download.pdf
byte[] pdfData= _policyGapManagerService.ExportPdf(id, out pdfName);//獲得pdf字節(jié)
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(pdfData)
};
result.Content.Headers.ContentDisposition =
new ContentDispositionHeaderValue("attachment")
{
FileName = pdfName
};
result.Content.Headers.ContentType =new MediaTypeHeaderValue("application/pdf");
return result;
}
二.返回pdfbyte數(shù)組
1.下載http模式的pdf文件(以ASP.NET為例,將PDF存在項(xiàng)目的目錄下,可以通過(guò)http直接打開(kāi)項(xiàng)目下的pdf文件)
#region 調(diào)用本地文件使用返回pdfbyte數(shù)組
/// <summary>
/// 調(diào)用本地文件使用返回pdfbyte數(shù)組
/// </summary>
/// <param name="srcPdfFile">‘D:\in2434341555551.pdf'</param>
/// <returns></returns>
public static byte[] GetSignaturePDFByte(string srcPdfFile)
{
using (FileStream fsRead = new FileStream(srcPdfFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
int fsLen = (int)fsRead.Length;
byte[] hebyte = new byte[fsLen];
fsRead.Read(hebyte, 0, hebyte.Length);
return hebyte;
}
}
#endregion 調(diào)用本地文件使用返回pdfbyte數(shù)組
#region 從網(wǎng)站上下載pdf,轉(zhuǎn)化為字節(jié)流
/// <summary>
/// 從網(wǎng)站上下載pdf,轉(zhuǎn)化為字節(jié)流
/// </summary>
/// <param name="srcPdfFile">文件地址:'https://******/group2/M00/00/04/wKj-mlpcoZ2IUbK5AACrpaV6k98AAAB6gAAAAAAAKu9562.pdf'</param>
/// <returns></returns>
public static Byte[] GetByteByRemoteURL(string srcPdfFile)
{
byte[] arraryByte;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(srcPdfFile);
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
StreamReader responseStream = new StreamReader(wr.GetResponseStream(), Encoding.UTF8);
int length = (int)wr.ContentLength;
byte[] bs = new byte[length];
HttpWebResponse response = wr as HttpWebResponse;
Stream stream = response.GetResponseStream();
//讀取到內(nèi)存
MemoryStream stmMemory = new MemoryStream();
byte[] buffer1 = new byte[length];
int i;
//將字節(jié)逐個(gè)放入到Byte 中
while ((i = stream.Read(buffer1, 0, buffer1.Length)) > 0)
{
stmMemory.Write(buffer1, 0, i);
}
arraryByte = stmMemory.ToArray();
stmMemory.Close();
}
return arraryByte;
}
#endregion 從網(wǎng)站上下載pdf,轉(zhuǎn)化為字節(jié)流
#region 從網(wǎng)站上下載文件,保存到其他路徑
/// <summary>
/// 從網(wǎng)站上下載文件,保存到其他路徑
/// </summary>
/// <param name="pdfFile">文件地址</param>
/// <param name="saveLoadFile">保存文件路徑:D:\12221.pdf</param>
/// <returns></returns>
public string SaveRemoteFile( string saveLoadFile , string pdfFile)
{
//bool flag = false;
var f = saveLoadFile + Guid.NewGuid().ToString("D") + ".pdf";
Uri downUri = new Uri(pdfFile);
//建立一個(gè)WEB請(qǐng)求,返回HttpWebRequest對(duì)象
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(downUri);
//流對(duì)象使用完后自動(dòng)關(guān)閉
using (Stream stream = hwr.GetResponse().GetResponseStream())
{
//文件流,流信息讀到文件流中,讀完關(guān)閉
using (FileStream fs = File.Create(f))
{
//建立字節(jié)組,并設(shè)置它的大小是多少字節(jié)
byte[] bytes = new byte[102400];
int n = 1;
while (n > 0)
{
//一次從流中讀多少字節(jié),并把值賦給N,當(dāng)讀完后,N為0,并退出循環(huán)
n = stream.Read(bytes, 0, 10240);
fs.Write(bytes, 0, n); //將指定字節(jié)的流信息寫(xiě)入文件流中
}
}
}
//return flag;
//return _outPath + saveLoadFile;
return f;
}
#endregion 從網(wǎng)站上下載文件,保存到其他路徑
2.ftp模式的pdf文件
/// <summary>
/// 下載FTP文件。
/// </summary>
/// <param name="offsetPath">相對(duì)路徑</param>
/// <param name="fileName">文件名稱(chēng)</param>
/// <returns>下載結(jié)果,本地文件路徑</returns>
public string DownLoad(string offsetPath,string fileName)
{
try
{
FtpWebRequest ftpWeb = (FtpWebRequest)WebRequest.Create(_ftpRootPath + offsetPath + fileName);
ftpWeb.Method = WebRequestMethods.Ftp.DownloadFile;
ftpWeb.UseBinary = true;
var resp = ftpWeb.GetResponse();
using (FileStream fs = new FileStream(_outPath + fileName, FileMode.Create))
{
using (var s = resp.GetResponseStream())
{
if (s == null) { return "文件不存在!"; }
int readCout = 0;
byte[] bytes = new byte[1024];
readCout = s.Read(bytes, 0, 1024);
while (readCout > 0)
{
fs.Write(bytes, 0, readCout);
readCout = s.Read(bytes, 0, 1024);
}
}
}
resp.Close();
return _outPath + fileName;
}
catch (Exception e)
{
return e.Message;
}
}
/// <summary>
/// 判斷文件是否存在
/// </summary>
/// <param name="offsetPath"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public bool FileExists(string offsetPath, string fileName)
{
try
{
FtpWebRequest ftpWeb = (FtpWebRequest)WebRequest.Create(_ftpRootPath + offsetPath + fileName);
ftpWeb.Method = WebRequestMethods.Ftp.DownloadFile;
ftpWeb.UseBinary = true;
var resp = (FtpWebResponse)ftpWeb.GetResponse();
resp.Close();
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 獲取目錄下所有文件
/// </summary>
/// <returns></returns>
public string[] Files(string offsetPath)
{
try
{
FtpWebRequest ftpWeb = (FtpWebRequest)WebRequest.Create(_ftpRootPath + offsetPath);
ftpWeb.Method = WebRequestMethods.Ftp.ListDirectory;
Stream stream = ftpWeb.GetResponse().GetResponseStream();
if (stream == null)
{
return null;
}
List<string> fileList = new List<string>();
using (StreamReader sr = new StreamReader(stream))
{
StringBuilder sb = new StringBuilder();
do
{
sb.Append(sr.ReadLine());
if (sb.Length > 0)
{
fileList.Add(sb.ToString());
sb.Clear();
}
else
{
break;
}
} while (true);
}
return fileList.ToArray();
}
catch (Exception)
{
return null;
}
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
C#編程簡(jiǎn)單實(shí)現(xiàn)生成PDF文檔的方法示例
這篇文章主要介紹了C#編程簡(jiǎn)單實(shí)現(xiàn)生成PDF文檔的方法,結(jié)合實(shí)例形式分析了C#生成PDF文檔的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-07-07
Unity3D UI Text得分?jǐn)?shù)字增加的實(shí)例代碼
這篇文章主要介紹了Unity3D UI Text得分?jǐn)?shù)字增加方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
C#自定義控件實(shí)現(xiàn)TextBox禁止粘貼的方法
這篇文章主要介紹了C#自定義控件實(shí)現(xiàn)TextBox禁止粘貼的方法,結(jié)合具體實(shí)例形式分析了C#自定義控件的創(chuàng)建、使用方法及TextBox禁止粘貼的實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06
c#集合快速排序類(lèi)實(shí)現(xiàn)代碼分享
這篇文章主要介紹了C#實(shí)現(xiàn)集合排序類(lèi),大家參考使用吧2013-12-12
C#中免費(fèi)密碼庫(kù)BouncyCastle的使用詳解
這篇文章主要來(lái)和大家分享一個(gè)C#版開(kāi)源、免費(fèi)的Bouncy?Castle密碼庫(kù):BouncyCastle,文中介紹了BouncyCastle的具體使用,需要的可以參考下2024-03-03
C#實(shí)現(xiàn)身份證驗(yàn)證功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)身份證驗(yàn)證功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
C#程序執(zhí)行時(shí)間長(zhǎng)查詢(xún)速度慢解決方案
這篇文章主要介紹了C#程序執(zhí)行時(shí)間長(zhǎng)查詢(xún)速度慢解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
C#里SuperSocket庫(kù)不能發(fā)現(xiàn)命令的原因
這篇文章主要介紹C#里SuperSocket庫(kù)不能發(fā)現(xiàn)命令的原因,在使用SuperSocket來(lái)寫(xiě)服務(wù)器的過(guò)程中,這是一個(gè)非??焖俚拈_(kāi)發(fā)方式,也非常好用。不過(guò)學(xué)習(xí)的曲線有點(diǎn)高,在使用的過(guò)程中經(jīng)常會(huì)遇到各種各樣的問(wèn)題。下面來(lái)看看學(xué)習(xí)舉例說(shuō)明吧2021-10-10

