Gridview Download not working properly in ASP.NET

BeUnique 2,332 Reputation points
2023-10-11T12:31:28.85+00:00

I'm developing asp.net page and it contains gridview.

Inside the gridview, i used link button for downloading the file.

The file contains in the File Server path (another server)

File path like ==> filename : "\DocServer121\Docs\S1Proj\EmpReview.xlsx"

i used below code and it is not working

Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim btnDownload As LinkButton = TryCast(sender, LinkButton)
        Dim gvrow As GridViewRow = TryCast(btnDownload.NamingContainer, GridViewRow)
        Dim fileName As String = grdResult.DataKeys(gvrow.RowIndex).Values(1).ToString()
        Response.ContentType = "image/jpg"
        Response.AddHeader("Content-Disposition", "attachment;filename=""" & fileName & """")
        Response.TransmitFile((fileName))
        Response.[End]()
End Sub

The downloading file is located in another server and not in the same server which is available in the application

Developer technologies | ASP.NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2023-10-13T10:00:52.0366667+00:00

    Hi @Gani_tpt,

    I'm just trying sample file from my system. but, nothing will happen in the browser.

    Were you able to successfully get images from another servers?

    can i have sample gridview download file wiht local and physical path of the server..?

    If you want to work on your local system, you can upload the image to your current solution and then download it. Examples are as follows:

    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="UploadFile" />
    <hr />
    <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>
    
     Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
         If Not IsPostBack Then
             Dim filePaths() As String = Directory.GetFiles(Server.MapPath("~/Uploads/"))
             Dim files As List(Of ListItem) = New List(Of ListItem)
             For Each filePath As String In filePaths
                 files.Add(New ListItem(Path.GetFileName(filePath), filePath))
             Next
             GridView1.DataSource = files
             GridView1.DataBind()
         End If
     End Sub
     Protected Sub UploadFile(ByVal sender As Object, ByVal e As EventArgs)
         Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
         FileUpload1.PostedFile.SaveAs((Server.MapPath("~/Uploads/") + fileName))
         Response.Redirect(Request.Url.AbsoluteUri)
     End Sub
     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
     Protected Sub DeleteFile(ByVal sender As Object, ByVal e As EventArgs)
         Dim filePath As String = CType(sender, LinkButton).CommandArgument
         File.Delete(filePath)
         Response.Redirect(Request.Url.AbsoluteUri)
     End Sub
    

    If you want to upload and download files from a remote server in ASP.NET, you can use the WebClient class. Detailed information can be found in this document.

    How to upload and download files from a remote server in ASP.NET

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.