How to download file without full file path using Response TransmitFile in ASP.NET

BeUnique 2,332 Reputation points
2022-06-28T05:50:18.33+00:00

I am downloading the file using the below code. But, downloading the file is showing the full path of the filename.
Example :
Filaname : Test.asd
File path : \tdsment.tds.intranet\USA\PART1\2022\
current download file (after downloading) : _tdsment.tds.intranet_USA_PART1_2022_Test.asd
I don't want to show the full file path after downloading the file.
Simply my downloaded file should be "Test.asd"
It means Expecting download file name : Test.asd

Public Sub TreeView1_SelectedNodeChanged(sender As Object, e As EventArgs)  
        Dim ChildNode1 As String = TreeView1.SelectedValue  
        filename = GetFileName(ChildNode1)  
        Response.Clear()  
        Using oBinaryReader As BinaryReader = New BinaryReader(File.OpenRead(filename))  
            Response.ContentType = "application/octet-stream"  
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename)  
            Response.TransmitFile(filename)  
            Response.Flush()  
        End Using  
End Sub  
Private Function GetFileName(ChildNode1 As String) As String  
        filepath = "\\tdsment.tds.intranet\USA\PART1\2022\"  
        Dim Result As String  
        Result = String.Concat(filepath, ChildNode1)  
        Return Result  
End Function  
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2022-06-28T11:27:18.34+00:00

    Path.GetFileName() returns the file name.

    Response.AppendHeader("Content-Disposition", "attachment; filename=" + System.IO.Path.GetFileName(filename))  
    

    Reference documentation.

    Path.GetFileName Method

    Or maybe the ChildNode1 variable contains the file name?


1 additional answer

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,096 Reputation points
    2022-06-28T07:56:38.14+00:00

    Hi @BeUnique ,
    If you don't have the full path route, you can't download the file. So, your requirement can't be realized. You could rename the filename. As your codes, rename the filename same as "ChildNode". So , Please delete "String.Concat".
    Best regards,
    Yijing Sun


    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.


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.