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);
}
}
查看全文
相关阅读:
SQL SERVER 存储过程或触发器等优化SET NOCOUNT ON
C#以16进制接收串口数据
DevExpress中的RichEditControl(富文本)控件,如何把滚动条移动至最后!
DevExpress中XtraReport的XRRichText在打印时,打印不出内容问题
DevExpress控件GridControl如何在页脚进行汇总
创建虚拟机SQL server
接口,抽象类
Linq 优化
sql 优化
C#:Hashtable和Dictionary
原文地址:https://www.cnblogs.com/ajaxleoxu/p/1089996.html
最新文章
vi和vim的三种模式
远程登录
linux用户管理
linux目录介绍
ABP VNext 微服务搭建系列文章
Visual Studio 实用功能(一) 开发快捷键
ABP VNext 微服务搭建入门(5)-- 网关
ABP VNext 微服务搭建入门(4)-- 微服务
ABP VNext 微服务搭建入门(3)-- 业务逻辑写在哪里
ABP VNext 微服务搭建入门(2)-- 从领域开始对象建模
热门文章
ABP VNext 微服务搭建入门(1)-- 模块分层架构
在sqlserver 的函数或存储过程中抛出异常(raiserror )【转载】
SQL server中获取语句执行时间(转载)
个人常用的Linux命令记录
Linux常用命令大全(非常全!!!)(转载)
Excel 导入 Sql Server出错——“文本被截断,或者一个或多个字符在目标代码页中没有匹配项”错误的解决(转载)
C# 四舍五入 保留两位小数(转载)
SQL语句 合并列值 将一列的多个值合并成一行
SQL Server 利用Replace函数实现不足长度左补0
Linux系统重启后 Docker服务及容器自动启动设置
Copyright © 2011-2022 走看看