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);
}
}
查看全文
相关阅读:
Java Web之Tomcat
Java Web之HTML5
Java 读取propertoes文件
SQL Server 触发器
通过关闭 UseDNS和GSSAPIAuthentication选项加速 SSH登录
数据库创建索引有什么优点和缺点
修改查看MYSQL字符集(charset)
MySQL 数据类型
远程连接mysql数据库提示:ERROR 1130的解决办法
怎么快速了解自己的MySQL服务器?
原文地址:https://www.cnblogs.com/ajaxleoxu/p/1089996.html
最新文章
Cocoapods
Swift
Swift
Mac
Swift
blocked because of many connection errors; unblock with 'mysqladmin flush-hosts;MySQL在远程访问时非常慢的解决方法;MySql链接慢的解决方法
转:Entity Framework对NULL值的处理
转:整理一下Entity Framework的查询
可选参数与命名参数;可变数量的参数
jquery 获取点击事件的id;jquery如何获取当前触发事件的控件ID值
热门文章
如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)?
java基础:数据类型
转载:NPOI导出到Excel表格
java基础 (记事本编写hello world,path,classpath,java的注释符)
Java Web之Servlet的三大作用域对象
Java Web之Web组件之间的跳转方式
Java Web之Cookie、Session
Java Web之Servlet
Java Web之Http协议
Idea使用Maven创建Java Web项目
Copyright © 2011-2022 走看看