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

遛弯鱼 2014-11-03 05:00:20
文件上传到内网,但对外网提供文件下载,不允许外网直接输入路径访问,但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等,最好在我基础上写上源码
...全文
270 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
遛弯鱼 2014-11-03
  • 打赏
  • 举报
回复
@sp1234 受教了,结贴给分
  • 打赏
  • 举报
回复
引用 4 楼 peng_bin1989 的回复:
啊哈,果然BinaryWrite可以的~
Stream ostream = HttpWebResponseUtility.CreateGetHttpResponse(Constant.GetInnerFileServerPath() + filePath, 2000, null, null).GetResponseStream();
List<byte> bytes = new List<byte>();
int temp = ostream.ReadByte();
while (temp != -1)
{
  bytes.Add((byte)temp);
  temp = ostream.ReadByte();
}       
其实人家 asp.net 早就有的 TransmitFile(filename) 方法比你的功能更强大,人家可以支持大文件输出(少占用内存),而且可以自动检测客户端是否连线。
  • 打赏
  • 举报
回复
嗯。删除点东西有好处。想得太多了反而出错。
遛弯鱼 2014-11-03
  • 打赏
  • 举报
回复
啊哈,果然BinaryWrite可以的~
Stream ostream = HttpWebResponseUtility.CreateGetHttpResponse(Constant.GetInnerFileServerPath() + filePath, 2000, null, null).GetResponseStream();
List<byte> bytes = new List<byte>();
int temp = ostream.ReadByte();
while (temp != -1)
{
  bytes.Add((byte)temp);
  temp = ostream.ReadByte();
}       
contentType = "application/octet-stream";
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.BinaryWrite(bytes.ToArray());
Response.Flush();
Response.End();
moonwrite 2014-11-03
  • 打赏
  • 举报
回复
只要你的HttpWebResponseUtility不是异步的 那么接下来就是将Stream 转成byte[]的 百度一下 反正思想就是电脑的一切数据都可以读成byte[]的 然将byte[]输出就是
遛弯鱼 2014-11-03
  • 打赏
  • 举报
回复
bytes是怎样获得的 Stream ostream = HttpWebResponseUtility.CreateGetHttpResponse(Constant.GetInnerFileServerPath() + filePath, 2000, null, null).GetResponseStream(); (红色部分是内网地址 例如http://192.168.1.2) 最原始的流是Stream网络流 是无法获得流长度的
moonwrite 2014-11-03
  • 打赏
  • 举报
回复
Response.Charset = "UTF-8"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); Response.ContentType = "application/octet-stream";//不用判断后缀名选择不同的type Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fileName)); Response.BinaryWrite(bytes); Response.Flush(); Response.End();

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧