简体   繁体   中英

Unable to download File in VB ASP.NET

Uncaught Sys.WebForms.PageRequestManagerParserErrorException: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

Getting this Error when I Click on download Button.

ASPX.cs

<asp:UpdatePanel runat="server" ID="UpdatePanelContent" UpdateMode="Conditional">
    <Triggers>
        <asp:PostBackTrigger ControlID="btnupload" />
    </Triggers>
    <ContentTemplate>
        <asp:FileUpload runat="server" ID="FileUpload1" value="Upload" />
        <asp:Button ID="btnupload" runat="server" OnClick="button1_click" Text="Upload"></asp:Button>
        <%-- <asp:LinkButton ID="OnLnkDownload" runat="server" OnClick="OnLnkDownload_Click" Font-Underline="False">Download</asp:LinkButton>--%>
    </ContentTemplate>
</asp:UpdatePanel>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText="No files uploaded">
    <Columns>
        <asp:BoundField DataField="Text" HeaderText="File Name" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkDownload" Text="Download" CommandArgument='<%# Eval("Value") %>' runat="server" OnClick="DownloadFile"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkDelete" Text="Delete" CommandArgument='<%# Eval("Value") %>' runat="server" OnClick="DeleteFile" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code Behind

Protected Sub DownloadFile(ByVal sender As Object, ByVal e As EventArgs)
    Dim filePath As String = CType(sender, LinkButton).CommandArgument
    Response.ContentType = ContentType
    Response.AppendHeader("Content-Disposition", ("attachment; filename=" + Path.GetFileName(filePath)))
    Response.WriteFile(filePath)
    Response.End()
End Sub

Please Help where I am getting wrong?

Make your download button a true link to an HttpHandler rather than a LinkButton. Right now your update panel tries to parse your file response in the client JavaScript as HTML or something. Even if it worked it would still be inferior to actual link to a dedicated URL for the file.

To do that you add a HttpHanler to your project. You put the code for the file download in the handler's ProcessRequest method.

Response.ContentType = ContentType
Response.AppendHeader("Content-Disposition", ("attachment; filename=" + Path.GetFileName(filePath)))
Response.WriteFile(filePath)
Response.End()

Now to pass the arguments you pass them in the URL say /MyHandler.ashx?filePath=someFile and use the QueryString property instead of CommandArgument. For example QueryString["filePath"]. Note that the handler should check for the appropriate permissions if they apply. In your page you use a HyperLink control and set the NavigateUrl to a URL towards your handler. You can test both parts separately. First get the Handler working and test by typing the URL in the browser and then generate the proper URL in your page for the HyperLink.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM