Results 1 to 3 of 3

Thread: Downloading File from Server to Client in asp.net

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    71

    Downloading File from Server to Client in asp.net

    hello everybody


    i have a asp.net web application running on my IIS server.now when a client machine on the local network connects to the server and tries to download a file the file gets downloaded on the server instead of the client mahcine.on my server i have the following code for downloading file

    imports System.net

    dim wc as webclient
    wc=new webclient
    wc.downloadfile("http:\\localhost\Download\test.txt","c:\test.txt")

    i want the test.txt to be saved on the c: drive of client machine however right now it is getting saved on the server.plz tell me how do i sort this out.

    thanx
    parth

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Downloading File from Server to Client in asp.net

    VB Code:
    1. Dim FilePath As String = strFileName
    2.         Dim TargetFile As New System.IO.FileInfo(FilePath)
    3.  
    4.         ' clear the current output content from the buffer
    5.         Response.Clear()
    6.         ' add the header that specifies the default filename for the Download/
    7.         ' SaveAs dialog
    8.         Response.AddHeader("Content-Disposition", "attachment; filename=" + _
    9.             TargetFile.Name)
    10.         ' add the header that specifies the file size, so that the browser
    11.         ' can show the download progress
    12.         Response.AddHeader("Content-Length", TargetFile.Length.ToString())
    13.         ' specify that the response is a stream that cannot be read by the
    14.         ' client and must be downloaded
    15.         Response.ContentType = "application/octet-stream"
    16.         ' send the file stream to the client
    17.         Response.WriteFile(TargetFile.FullName)
    18.         ' stop the execution of this page
    19.         Response.End()

    You cannot specify where this will get saved, it is at the discretion of the user.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Downloading File from Server to Client in asp.net

    So you would place that in a page's codebehind and call that page like

    Response.Redirect("Download.asxp?file=abc.txt");

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width