Tuesday, December 4, 2007

How to create a downloadable file

How to create a downloadable file

To create a downloadable file

Let’s say that you want to export your data from SQL Server to a file and make it available for downloading. Here’s how you should do it.

<%

If Request.Form("action") = "Download File" Then

shortfile = "authors" & Session.SessionID & ".xls"

file = Server.MapPath("/virtualpath") & Session.SessionID & ".xls"

strSQL = "EXEC xp_cmdshell '" & _

"bcp pubs..authors out " & file & " -c -Sservernameorip -Uusername -Ppassword'"

strConn = "Provider=SQLOLEDB;Data Source=mydb;User ID=userid;Password=password"

Set objConn = Server.CreateObject("ADODB.Connection")

objConn.Open strConn

objConn.Execute(strSQL)

objConn.Close

Set objConn = Nothing

Set objFS = Server.CreateObject("Scripting.FileSystemObject")

If objFS.FileExists(file) Then

%>"%>Download Now <%

End If

Set objFS = Nothing

End If

%>

No comments: