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
C# 将PDF文件下载到本地PC时出现问题_C#_Asp.net_Pdf_Pdfsharp - Fatal编程技术网

C# 将PDF文件下载到本地PC时出现问题

C# 将PDF文件下载到本地PC时出现问题,c#,asp.net,pdf,pdfsharp,C#,Asp.net,Pdf,Pdfsharp,我正在尝试从Web服务器下载使用PDFsharp创建的PDF文件,并将其下载到本地PC的downloadfolder。我在谷歌上搜索了很多,尝试了很多,但都没有成功。我可以正确创建PDF文件并将其保存到磁盘,但我无法将其传输/下载到本地PC的下载文件夹 我尝试了以下代码: string filename = Server.MapPath("~\\Files\\Temp\\file.pdf"); document.Save(filename); //generate the physical fi

我正在尝试从Web服务器下载使用PDFsharp创建的PDF文件,并将其下载到本地PC的downloadfolder。我在谷歌上搜索了很多,尝试了很多,但都没有成功。我可以正确创建PDF文件并将其保存到磁盘,但我无法将其传输/下载到本地PC的下载文件夹

我尝试了以下代码:

string filename = Server.MapPath("~\\Files\\Temp\\file.pdf");
document.Save(filename); //generate the physical file

MemoryStream stream = new MemoryStream();
document.Save(stream, false);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", stream.Length.ToString());
Response.BinaryWrite(stream.ToArray());
Response.Flush();
stream.Close();
Response.End();

那么我做错了什么呢?

缺少
内容配置

Response.AddHeader("Content-Disposition", "attachment;filename=test.pdf"); 
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Length", binfile.Length.ToString());
Response.BinaryWrite(binfile);
如果有更新面板,则必须将按钮设置为回发触发器

  <asp:UpdatePanel runat="server">
        <Triggers>
            <asp:PostBackTrigger ControlID="btnID" />
        </Triggers>
        <ContentTemplate></ContentTemplate>
    </asp:UpdatePanel>

缺少
内容处置

Response.AddHeader("Content-Disposition", "attachment;filename=test.pdf"); 
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Length", binfile.Length.ToString());
Response.BinaryWrite(binfile);
如果有更新面板,则必须将按钮设置为回发触发器

  <asp:UpdatePanel runat="server">
        <Triggers>
            <asp:PostBackTrigger ControlID="btnID" />
        </Triggers>
        <ContentTemplate></ContentTemplate>
    </asp:UpdatePanel>


代码运行时会发生什么?什么不起作用?@NateBarbettini什么也没发生-我看不到下载文件夹中的文件:-(@MichaelEriksen您是否尝试过在代码中添加日志记录或使用断点调试程序?如果是,结果是什么。前两行毫无意义。当
MemoryStream
是您所需要的时,为什么要写入本地文件?可能此处显示的代码片段周围的代码有问题。@IliassNassibane我已经调试了prgram没有成功。我在资源管理器中跟踪了它-但什么也没发生:-(代码运行时会发生什么?什么不起作用?@NateBarbettini什么也没发生-我在下载文件夹中看不到文件:-(@MichaelEriksen您是否尝试过在代码中添加日志记录或使用断点调试程序?如果是,结果是什么。前两行毫无意义。当
MemoryStream
是您所需要的时,为什么要写入本地文件?可能此处显示的代码片段周围的代码有问题。@IliassNassibane我已经调试了prGram没有成功。我在Explorer中遵循了它-但什么也没有发生:-(不确定这是缺少的部分。官方web示例没有这一行:@DragandDrop正确链接。很可能这个示例是原始帖子中代码的原型(前两行没有意义除外)。我已尝试设置内容配置-但我仍然无法下载文件:-(不确定是否缺少这一点。官方网站示例没有这一行:@DragandDrop正确链接。此示例很可能是原始帖子中代码的原型(前两行没有意义除外)。我已尝试设置内容配置,但仍无法下载该文件:-(