zoukankan
html css js c++ java
生成PDF下载 HTTP或FTP远程获取PDF
这个只是些代码片断。 备用。希望也能对大家有用。
down.ashx.cs
public
void
ProcessRequest(HttpContext context)
{
string
title
=
sui.Title;
string
su
=
sui.SourceUrl;
//
全文下载链接地址
context.Response.Clear();
context.Response.ContentType
=
"
application/pdf
"
;
context.Response.ContentEncoding
=
Encoding.UTF8;
context.Response.Charset
=
"
utf-8
"
;
context.Response.AddHeader(
"
Content-Disposition
"
,
"
attachment; filename=
"
+
HttpUtility.UrlEncode(title
+
"
.pdf
"
,Encoding.UTF8));
if
(su.Contains(
"
ftp
"
))
{
string
userName
=
""
;
string
password
=
""
;
FtpDownload(context, su, userName, password);
}
else
{
HttpDownload(context, su);
}
}
private
static
void
FtpDownload(HttpContext context,
string
fileUrl,
string
userName,
string
password)
{
try
{
byte
[] result;
byte
[] buffer
=
new
byte
[
4096
];
Uri filepath
=
new
Uri(fileUrl);
FtpWebRequest reqFTP
=
(FtpWebRequest)FtpWebRequest.Create(filepath);
reqFTP.Method
=
WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary
=
true
;
reqFTP.Credentials
=
new
NetworkCredential(userName, password);
using
(FtpWebResponse response
=
(FtpWebResponse)reqFTP.GetResponse())
{
using
(Stream responseStream
=
response.GetResponseStream())
{
using
(MemoryStream memoryStream
=
new
MemoryStream())
{
int
count
=
0
;
do
{
count
=
responseStream.Read(buffer,
0
, buffer.Length);
memoryStream.Write(buffer,
0
, count);
}
while
(count
!=
0
);
result
=
memoryStream.ToArray();
HttpContext.Current.Response.AddHeader(
"
Content-Length
"
, result.Length.ToString());
memoryStream.WriteTo(context.Response.OutputStream);
memoryStream.Close();
context.Response.OutputStream.Flush();
context.Response.Flush();
}
}
}
}
catch
(Exception ex)
{
Exceptions.LogException(ex);
}
}
private
static
void
HttpDownload(HttpContext context,
string
httpUrl)
{
try
{
byte
[] buffer
=
new
byte
[
4096
];
HttpWebRequest reqHTTP
=
(HttpWebRequest)WebRequest.Create(httpUrl);
using
(HttpWebResponse response
=
(HttpWebResponse)reqHTTP.GetResponse())
{
using
(Stream responseStream
=
response.GetResponseStream())
{
using
(MemoryStream memoryStream
=
new
MemoryStream())
{
int
count
=
0
;
do
{
count
=
responseStream.Read(buffer,
0
, buffer.Length);
memoryStream.Write(buffer,
0
, count);
}
while
(count
!=
0
);
byte
[] result
=
memoryStream.ToArray();
HttpContext.Current.Response.AddHeader(
"
Content-Length
"
, result.Length.ToString());
memoryStream.WriteTo(context.Response.OutputStream);
memoryStream.Close();
context.Response.OutputStream.Flush();
context.Response.Flush();
}
}
}
}
catch
(Exception ex)
{
Exceptions.LogException(ex);
}
}
查看全文
相关阅读:
自动化运维工具Ansible实战Playbooks剧本使用
Mysql5.6.x版本半同步主从复制的开启方法
mysql数据库的一些基本概念
mysql之视图
mysql之索引
机器学习笔记09-----决策树与随机森林1---决策树概述
机器学习笔记08-----回归2
Anaconda 利用conda安装第3方包
机器学习笔记07-----回归1
使用navicat导出数据库字典
原文地址:https://www.cnblogs.com/ajaxleoxu/p/1089996.html
最新文章
10个扁平化设计网站
通过Ajax方式上传文件,使用FormData进行Ajax请求
HTML5 拖拽事件
Java和Js的高精度计算
表单文件上传,ajax文件上传
验证码
【转】JSP自定义标签
【转】Spring Bean属性解析
【转】深入理解Java的接口和抽象类
【转】spring 装配Bean中构造参数的注入
热门文章
【转】常见 jar包详解
ansible常用命令大全
[转]Java对Redis的基本原生操作
[转]storm的八种Grouping策略
[转]redis使用管道pipeline实现批量新增和修改
[转]Flume+Kafka+Flink+Redis构建大数据实时处理系统:实时统计网站PV、UV展示
Mysql 备份恢复与xtrabackup备份
Gitlab+Jenkins实现自动部署
Docker+Jenkins+GIT+Tomcat实战持续化集成
Ansible入门与playbook实战
Copyright © 2011-2022 走看看