question: how to implement a simple file download

Discussion in 'ASP.NET 2.0' started by wjprice, Jan 6, 2007.

  1. I am a newbie at setting up web sites. My current problem is finding info on how to implement a file download link. I want the user to click the link and the file (a MS Access database) should download. It does not need authentication or anything else.
    If there is anyone who can assist me I would greatly appreciate it.
    Thanks,
    Willie
     
  2. The most simple method is still a "a href" where you link to the file.


    For a database download it would be best to compress the file, ZIP or RAR formats both work well.


    An href would look like this for the data file located on the root:


    <div><a href="/data.mdb">data.mdb</a></div>
     
  3. Try This...

    Dim fs As FileStream
    Dim strContentType As String
    Dim strPath = 'server.mappath('~\myFileDir\myFile')
    Dim strFileName As String = 'myFileToDownload.txt'
    fs = File.Open(strPath & strFileName, FileMode.Open)
    Dim bytBytes(fs.Length) As Byte
    fs.Read(bytBytes, 0, fs.Length)
    fs.Close()
    Response.AddHeader('Content-disposition', 'attachment; filename=' & strFileName)
    Response.ContentType = 'application/octet-stream'
    Response.BinaryWrite(bytBytes)
    Response.End()

    kevinasp.com
     

Share This Page