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);
}
}
查看全文
相关阅读:
PHP之Trait详解
PHP中__call()方法与重载解析
PHP Closure(闭包)类详解
PHP 核心特性
回调函数
php的各种 I/O流 以及用法
关于php的buffer(缓冲区)
php的运行原理、cgi对比fastcgi以及php-cgi和php-fpm之间的联系区别
低功耗设计入门(一)——低功耗设计目的与功耗的类型
从CMOS到触发器(一)
原文地址:https://www.cnblogs.com/ajaxleoxu/p/1089996.html
最新文章
Python中列表,元祖,字典和集合的异同
斐波那契数列
线程安全的单例模式
Python列表合并、去重和排序
JS求字符串长度
JS去重 + Python去重
JS快排
欧几里得算法
MySQL索引背后的数据结构及算法原理
有关各种神经网络精度
热门文章
Ubuntu使用命令清空回收站
腾讯文档使用记录
夜深了
即使是昏暗的街道,也有每日照耀的月
鞋子的保养
Linux FTP安装问题
python读取csv文件
Numba 开发手册(一)
php 接触的不常见函数
php 中__set()和__get()的具体用法
Copyright © 2011-2022 走看看