服务器端提供pdf文件下载,非直接访问模式

遛弯鱼 遛弯鱼 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
文件上传到内网,但对外网提供文件下载,不允许外网直接输入路径访问,但PDF怎样实现下载,现在代码如下
object content = string.Empty;
string commandArgument = e.CommandArgument.ToString();
string filePath = commandArgument;
string fileext = System.IO.Path.GetExtension(filePath);
string contentType = string.Empty;
if (fileext == ".doc" || fileext == ".docx")
{
_Application myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
_Document myWordDoc;
contentType = "application/msword";
myWordDoc = myWordApp.Documents.Open(Constant.GetInnerFileServerPath() + filePath);
content = myWordDoc.Content.Text;
myWordDoc.Close();
myWordApp.Quit();
}
else if (fileext == ".pdf")
{
StreamReader sr = null;
Stream ostream = HttpWebResponseUtility.CreateGetHttpResponse(Constant.GetInnerFileServerPath() + filePath, 2000, null, null).GetResponseStream();
sr = new StreamReader(ostream, System.Text.Encoding.UTF8);
string strContent= sr.ReadToEnd();
content = strContent;
contentType = "application/pdf";
}
Response.AppendHeader("Connection", "close");
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(System.IO.Path.GetFileName(filePath), System.Text.Encoding.UTF8));
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = contentType;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Write(content);
Response.Flush();
Response.End();

第一个doc能够实现下载,docx未能完全实现,第二个pdf解析出来总是中文乱码请求大神怎样才能实现下载,尝试过试用Itestsharp、Acrobat.dll等,最好在我基础上写上源码
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
遛弯鱼
C# 111,129 成员 642,538 内容