Search code examples
asp.netvb.netresponsetransmitfile

Why the TransmitFile is not downlaoding the file?


I am downloading a file from folder but this code of me doesn't download. It doesn't throw any error but doesn't download either.

  Dim req As WebClient = New WebClient()
        Dim response As HttpResponse = HttpContext.Current.Response
        Dim filePath As String = "~/Downloads/MyExcelFile.xls"
        response.Clear()
        response.ClearContent()
        response.ClearHeaders()
        response.Buffer = True
        response.AddHeader("Content-Disposition", "attachment;filename=Filename.extension")
        'Dim data As Byte() = req.DownloadData(Server.MapPath(filePath))
        'response.BinaryWrite(data)
        response.TransmitFile(Server.MapPath(filePath))
        'response.End()

Solution

  • try

    response.WriteFile("some file");
    response.Flush();
    response.Close();
    

    also consider Andrew's response on handler type - ashx is usually cleaner, with webform you might have other things that happen during page life cycle.