zoukankan      html  css  js  c++  java
  • pdf 下载整理

    pdf下载整理:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Net;
    
    namespace PdfDownload
    {
        /// <summary>
        /// PDF 下载工具
        /// </summary>
        public class PDF : IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
                var url = context.Request["url"];
                var title = context.Request["title"];
                var response = context.Response;
                if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(title))
                {
                    response.Charset = System.Text.Encoding.UTF8.ToString();
                    response.ContentEncoding = System.Text.Encoding.UTF8;
                    try
                    {
                        HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(url);
                        request.Timeout = 5000;
                        request.ReadWriteTimeout = 1000;
                        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
                        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
                        using (var remoteResponse = request.GetResponse())
                        {
                            var length = remoteResponse.ContentLength;
                            if (length != -1)
                            {
                                response.ContentType = "application/pdf";
                                response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(title));
                                response.AppendHeader("Content-Length", length.ToString());
                                byte[] buffer = new byte[512 * 1024];
                                using (var stream = remoteResponse.GetResponseStream())
                                {
                                    var position = 0;
                                    while ((position = stream.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        response.OutputStream.Write(buffer, 0, position);
                                    }
                                }
                                response.Flush();
                                return;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        response.ContentType = "html/text";
                        System.Text.StringBuilder builder = new System.Text.StringBuilder();
                        builder.Append("URL:");
                        builder.AppendLine(url);
                        builder.AppendLine(ex.ToString());
                        response.Write(builder.ToString());
                    }
                }
                else
                {
                    context.Response.StatusCode = 404;
                }
            }
    
            public string GetWindSessionID(HttpContext context)
            {
                string str = context.Request.Headers["wind.sessionid"];
                if (string.IsNullOrEmpty(str))
                {
                    str = context.Request.QueryString["wind.sessionid"];
                    if (string.IsNullOrEmpty(str) && (context.Request.Cookies["wind.sessionid"] != null))
                    {
                        str = context.Request.Cookies["wind.sessionid"].Value;
                    }
                }
                return str;
            }
    
            public bool IsReusable
            {
                get
                {
                    return true;
                }
            }
        }
    }
    

      

  • 相关阅读:
    dedecms自定义表单提交获取时间跟ip地址
    JQuery购物车多物品数量的加减+总价计算
    jquery手机触屏滑动拼音字母城市选择器代码
    js实现图片上传实时显示
    js实现发送验证码倒计时效果
    JS 仿支付宝input文本输入框放大组件
    js实现倒计时效果
    jquery统计输入文字的个数并对其进行判断
    【Linux】Linux系统安全设置
    java泛型(generics)
  • 原文地址:https://www.cnblogs.com/fo0ol/p/8784823.html
Copyright © 2011-2022 走看看