下载pdf文件时出错 [英] error when downloading a pdf file

查看:111
本文介绍了下载pdf文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我必须从内容页面上单击链接按钮,然后下载pdf文件.
我正在使用以下代码:


i have to download a pdf file on a link button click from a content page.
i am using following code :

Response.ContentType = "Application/pdf";
        Response.AppendHeader("Content-Disposition", "attachment; filename=Softgen.pdf");
        Response.TransmitFile(Server.MapPath("Hosts_Win_Brochure.pdf"));
        Response.End();



但它给出了错误:



but it is giving error:

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.<br />
Details: Error parsing near ''%PDF-1.5<br />
%��<br />
4 0 ob''

.

相同的代码在正常页面上正常工作.

.

Same code is working with normal page.

推荐答案

嗨 纳文,

您可以通过传递文件路径来使用以下功能
Hi Naveen,

you can do using following function with passing file path
Public Shared Function DownloadFile(ByVal filePath As String) As Boolean
        Try
            If File.Exists(HttpContext.Current.Server.MapPath(filePath)) Then
                Dim strFileName As String = ""
                strFileName = System.IO.Path.GetFileName(HttpContext.Current.Server.MapPath(filePath))
                strFileName = strFileName.Replace(" ", "%20")
                HttpContext.Current.Response.ContentType = "application/octet-stream"
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" & strFileName) ' System.IO.Path.GetFileName(HttpContext.Current.Server.MapPath(filePath)))
                HttpContext.Current.Response.Clear()
                HttpContext.Current.Response.WriteFile(HttpContext.Current.Server.MapPath(filePath))
                HttpContext.Current.Response.End()
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
        End Try
    End Function




如有任何疑问,请让我知道.

请提供"投票":thumbsup:如果有帮助,请提供"接受答案",如果这是正确的答案.:rose:

谢谢,
Imdadhusen




Please do let me know, if you have any doubt.

Please provide "Vote":thumbsup: if this would be helpful, and make "Accept Answer" if this would be correct answer.:rose:

Thanks,
Imdadhusen


这篇关于下载pdf文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆