Script(s)

what I learn is what u c

ASP.Net : Force File Download

with 3 comments

‘ Stream File Content to Browser

Sub Download(ByVal sPath As String, ByVal iFormCode As Integer, ByVal sExt As String)

Dim sFileName As String
sFileName = CType(iFormCode, String) & “.” & sExt
‘Dim filename As String = sPath
Response.Clear()
If sExt.ToLower = “pdf” Then
Response.ContentType = “application/pdf”
ElseIf sExt.ToLower = “doc” Then
Response.ContentType = “application/doc”
End If
If File.Exists(sPath) Then
Response.AddHeader(“Content-Disposition”, “attachment; filename=””” & sFileName & “”””)
Response.WriteFile(sPath)
Response.Flush()
Else
Page.RegisterStartupScript(“notfound”, “[javascript function]”)
End If

End Sub

Advertisements

Written by gchandra

February 15, 2006 at 5:32 pm

Posted in Web Coding

3 Responses

Subscribe to comments with RSS.

  1. Pdf Files are downloaded in the system but give error at opening time that file is damaged, other files works good, how to solve theis problem.

    Anwar

    September 13, 2006 at 12:32 am

  2. Anwar :

    Sometimes this happen when acrobat is trying to update itself when opening it. If that is your case try updating it first and try this one.

    ————-

    There is another (similar to this one) way of doing the same. This i used for csv you can change it to PDF.

    https://gchandra.wordpress.com/2005/05/03/aspnet-vb-write-to-memory-stream/

    gchandra

    September 13, 2006 at 4:40 pm

  3. here a complete vb.net example to download files http://it-things.com/index.php/2011/04/asp-net-force-file-download/

    It also explains some interesting security issues.

    mred

    April 28, 2011 at 8:41 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: