Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何将文件发送到客户端以打开下载对话框?_Asp.net_Asp.net 3.5 - Fatal编程技术网

Asp.net 如何将文件发送到客户端以打开下载对话框?

Asp.net 如何将文件发送到客户端以打开下载对话框?,asp.net,asp.net-3.5,Asp.net,Asp.net 3.5,我有一个文件,比如说我的网站上有一个PDF,当用户访问一个页面时,我想在页面加载时显示一个PDF下载对话框,或者单击一个按钮 我在谷歌上搜索了一下,我发现了两种方法,但我想知道什么是公认的方法?我现在正在做这个 string pdfPath = MapPath("mypdf.pdf"); Response.ContentType = "Application/pdf"; Response.AppendHeader( "content-disposition", "attach

我有一个文件,比如说我的网站上有一个PDF,当用户访问一个页面时,我想在页面加载时显示一个PDF下载对话框,或者单击一个按钮

我在谷歌上搜索了一下,我发现了两种方法,但我想知道什么是公认的方法?我现在正在做这个

string pdfPath = MapPath("mypdf.pdf");
Response.ContentType = "Application/pdf";


Response.AppendHeader( "content-disposition",
        "attachment; filename=" + name );
Response.WriteFile(pdfPath);
Response.End();
代码基于中的代码,也从中找到代码
您的代码将完美地向用户显示文件。但他们必须使用“另存为”选项来实际保存它

如果要向用户显示“保存”对话框,请尝试以下操作:

string pdfPath = MapPath("mypdf.pdf");
Response.ContentType = "Application/pdf";
Response.AppendHeader("content-disposition",
        "attachment; filename=" + pdfPath );
Response.TransmitFile(pdfPath);
Response.End();

当然,这假定文件实际上存在于服务器上,并且不是动态生成的。

此代码将直接在客户端浏览器上将任何文件发送到

    Response.ContentType = "application/pdf";
    Response.WriteFile(PathToFile);
    Response.Flush();