Jagadish's profileJagadish Pulakhandam'sPhotosBlogListsMore Tools Help

Blog


    May 18

    TIP: File download using ASP.NET

    The following is a method to send a file to browser using ASP.NET
     
    Dim fs As FileStream
    Dim strPath = "C:\DownloadableFilePath\"
    Dim strFileName As String = "Filename.pdf"
    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() 'or Response.Redirect("somepage.aspx",false)
     
    Alternatively, you can also use (if it is available within web application folders) with WriteFile() as follows
     
    Public Sub SecureFileDownload(ByVal inFile As String)
        Dim strFileNamePath As String
        strFileNamePath = Request.MapPath("SecureDirectory") & "\" & inFile
        Dim myFile As FileInfo = New FileInfo(strFileNamePath)
        Response.Clear()
        Response.AddHeader("Content-Disposition", "attachment; filename=" & myFile.Name)
        Response.AddHeader("Content-Length", myFile.Length.ToString())
        Response.ContentType = "application/octet-stream"
        Response.WriteFile(myFile.FullName)
        Response.End()
    End Sub
     
    thanks
    Jag

    TIP: Developing ASP.NET 2.0 applications simultaneously with both C# and VB.NET

    If the code (say some classes) is written in C# and if the application is being developed in Visual Basic.NET, you can create a separate folder under the App_Code folder. The folder must be added as a code subdirectory in the web.config file as shown here:
    <compilation debug="true" strict="false" explicit="true">
      <codeSubDirectories>
        <add directoryName="ProfileProvider" />
      </codeSubDirectories>
    </compilation>
    

     

    By adding the folder as a code subdirectory, all C# files in the folder will be compiled separately from the rest of the Web application project. At run time, two DLLs will be generated in the ASP.NET temporary folder for the application.

    Enjoy,

    Jag