普通话件的下载

        //执行wkhtmltopdf.exe
        Process p =
System.Diagnostics.Process.Start(@”D:\wkhtmltopdf\wkhtmltopdf.exe”,
@”http://msdn.microsoft.com/zh-cn D:\” + fileNameWithOutExtention +
“.pdf”);

    //WriteFile达成下载
    protected void Button2_Click(object sender, EventArgs e)
    {
        /*
         using System.IO;
         
         */

        //因为Web
是二十多线程境况,幸免甲发生的文本被乙下载去,所以档名都用唯一
        string fileNameWithOutExtention = Guid.NewGuid().ToString();

        Response.ContentType = “application/x-zip-compressed”;
        Response.AddHeader(“Content-Disposition”,
“attachment;filename=z.zip”);
        string filename = Server.MapPath(“DownLoad/z.zip”);
        Response.TransmitFile(filename);
    }

        //把文件读进文件流
        FileStream fs = new FileStream(@”D:\” +
fileNameWithOutExtention + “.pdf”, FileMode.Open);
        byte[] file = new byte[fs.Length];
        fs.Read(file, 0, file.Length);
        fs.Close();

        if (fileInfo.Exists == true)
        {
            const long ChunkSize = 拾2400;//100K
每一次读取文件,只读取100K,那样能够化解服务器的下压力
            byte[] buffer = new byte[ChunkSize];

C# wrapper around excellent wkhtmltopdf console utility 
https://github.com/codaxy/wkhtmltopdf

种种实现文件下载的点子:

//Button的Click事件(把Url的网页内容转成PDF)
    protected void btn_execute_Click(object sender, EventArgs e)
    {

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

        //Response给客户端下载
        Response.Clear();
葡京手机登陆网址,        Response.AddHeader(“content-disposition”, “attachment;
filename=” + fileNameWithOutExtention + “.pdf”);//强制下载
        Response.ContentType = “application/octet-stream”;
        Response.BinaryWrite(file);

  • fileName);
            Response.AddHeader(“Content-Length”,
    fileInfo.Length.ToString());
            Response.AddHeader(“Content-Transfer-Encoding”, “binary”);
            Response.ContentType = “application/octet-stream”;
            Response.ContentEncoding =
    System.Text.Encoding.GetEncoding(“gb2312”);
            Response.WriteFile(fileInfo.FullName);
            Response.Flush();
            Response.End();
        }

public partial class _Default : System.Web.UI.Page
{

            Response.Clear();
            System.IO.FileStream iStream =
System.IO.File.OpenRead(filePath);
            long dataLengthToRead =
iStream.Length;//获取下载的文书总大小
            Response.ContentType = “application/octet-stream”;
            Response.AddHeader(“Content-Disposition”, “attachment;
filename=” + HttpUtility.UrlEncode(fileName));
            while (dataLengthToRead > 0 &&
Response.IsClientConnected)
            {
                int lengthRead = iStream.Read(buffer, 0,
Convert.ToInt32(ChunkSize));//读取的尺寸
                Response.OutputStream.Write(buffer, 0, lengthRead);
                Response.Flush();
                dataLengthToRead = dataLengthToRead – lengthRead;
            }
            Response.Close();
        }
    }

wkhtmltopdf,一个合并好了的exe文件(C++编写),基本的调用方法是,
wkhtmltopdf.exe http://passport.yupsky.com/ac
count/register e:\yupskyreg.pdf

 

,能够先在命令行测试一下,有其余的急需能够在命令行通过wkhtmltopdf
–help查询,假使是超长页的花,能够用命令

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

 

        string fileName =”asd.txt”;//客户端保存的文书名
        string filePath=Server.MapPath(“DownLoad/aaa.txt”);//路径

    }
}

    //TransmitFile达成下载
    protected void Button1_Click(object sender, EventArgs e)
    {
        /*
        
微软为Response对象提供了1个新的点子TransmitFile来化解接纳Response.BinaryWrite
        
下载超越400mb的文书时产生Aspnet_wp.exe进度回收而一筹莫展成功下载的标题。
         代码如下:
         */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/*要引用以下命名空间*/
using System.Diagnostics;
using System.IO;

        //以字符流的款式下载文件
        FileStream fs = new FileStream(filePath, FileMode.Open);
        byte[] bytes = new byte[(int)fs.Length];
        fs.Read(bytes, 0, bytes.Length);
        fs.Close();
        Response.ContentType = “application/octet-stream”;
        //通告浏览器下载文件而不是展开
        Response.AddHeader(“Content-Disposition”, “attachment; 
filename=” + HttpUtility.UrlEncode(fileName,
System.Text.Encoding.UTF8));
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();

Calling wkhtmltopdf to generate PDF from
HTML

老外最多少人加分的那篇做法,使用wkhtmtopdf(GPL协议)能够省诸多程序代码,
首先到官网http://code.google.com/p/wkhtmltopdf/downloads/list
找installer.exe下载

        System.IO.FileInfo fileInfo = new
System.IO.FileInfo(filePath);

       
//若不加那1行,程序就能够即时实施下一句而抓不到文件产生意外:System.IO.FileNotFoundException:
找不到文件 ”。
        p.WaitForExit();

    }
}

在GitHub上开掘3个有关的体系,其中Pechkin那些类型无需独自安装wkhtmltopdf
,就是.NET的库了。

    //流模式下载
    protected void Button4_Click(object sender, EventArgs e)
    {
        string fileName = “aaa.txt”;//客户端保存的公文名
        string filePath = Server.MapPath(“DownLoad/aaa.txt”);//路径

wkhtmltopdf.exe http://passport.yupsky.com/ac
count/register e:\yupskyreg.pdf  -H –outline
(-H是增添默许题目,–outline是增添pdf的右侧概要啊!)而且能够批量生成哦,中间用空格隔断葡京手机登陆网址 1

    //WriteFile分块下载
    protected void Button3_Click(object sender, EventArgs e)
    {

.NET Wrapper for WkHtmlToPdf static DLL. Allows you to utilize full
power of the libra:https://github.com/gmanny/Pechkin

    }

        string fileName = “aaa.txt”;//客户端保存的文书名
        string filePath = Server.MapPath(“DownLoad/aaa.txt”);//路径

        FileInfo fileInfo = new FileInfo(filePath);
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader(“Content-Disposition”, “attachment;filename=”