• 生成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);
          }

        }



  • 相关阅读:
    GNU软件FTP下载汇总
    设置git的代理服务器
    今天发现一个Convert.ToDateTime的异常,算不算微软的bug呢?
    无线电空间传输损耗衰减计算(转帖)
    使用ArcGis10.2通过Dem提取山顶点(原创)
    VC++编译zlib
    VC++编译libpng
    vc++编译libtiff4.0.4
    VC++编译GSL
    libCEF总结02字符串
  • 原文地址:https://www.cnblogs.com/ajaxleoxu/p/1089996.html
Copyright © 2020-2023  润新知