ASP.NET Problems downloading a file

Labeots

New Member
Hi all, <BR><BR>I've been struggling trying to get the following code to work, so I can pass the filename through a query.string and the page will serve the file. Here is the code:<BR><BR><%@ Import Namespace="System.IO"%><BR><script language="VB" runat="server"><BR>Sub Page_Load(sender As Object, e As EventArgs)<BR><BR> Dim filepath As String = "C:Inetpubwebsites est_files\"<BR> Dim filename As String = trim(request.QueryString("file"))<BR> Dim strFileType As String = lcase(Right(filename, 4))<BR> <BR> ' Feel Free to Add Your Own Content-Types Here<BR> Select Case strFileType<BR> Case ".asf"<BR> ContentType = "video/x-ms-asf"<BR> Case ".avi"<BR> ContentType = "video/avi"<BR> Case ".doc"<BR> ContentType = "application/msword"<BR> Case ".zip"<BR> ContentType = "application/zip"<BR> Case ".pdf"<BR> ContentType = "application/pdf" <BR> Case ".xls"<BR> ContentType = "application/vnd.ms-excel"<BR> Case ".gif"<BR> ContentType = "image/gif"<BR> Case ".jpg", "jpeg"<BR> ContentType = "image/jpeg"<BR> Case ".wav"<BR> ContentType = "audio/wav"<BR> Case ".mp3"<BR> ContentType = "audio/mpeg3"<BR> Case ".mpg", "mpeg"<BR> ContentType = "video/mpeg"<BR> Case ".rtf"<BR> ContentType = "application/rtf"<BR> Case ".htm", "html"<BR> ContentType = "text/html"<BR> Case ".asp"<BR> ContentType = "text/asp"<BR> Case Else<BR> 'Handle All Other Files<BR> ContentType = "application/octet-stream"<BR> End Select<BR> Response.Clear()<BR> Response.ContentType = "application/octet-stream"<BR> Response.AddHeader("Content-Disposition", _<BR> "attachment; filename=""" & filename & """")<BR> Response.Flush()<BR> Response.WriteFile(filepath)<BR><BR>End Sub<BR></script><BR><BR>When the page loads it actually prompts the user to download a file. But the document that is downloaded is empty?<BR><BR>Any ideas, thanks in advance<BR><BR>James<BR>You go to all the trouble to choose one of several different types and then just toss away all that work???<BR><BR>And you never declared a variable type for your ContentType variable.<BR><BR>And, finally, you try to use <BR> Response.WriteFile<BR>and pass it a *DIRECTORY* instead of a single file!<BR><BR>May not be all the problems, but it's a start.Maybe instead of:<BR><BR>Response.WriteFile(filepath)<BR><BR>try<BR><BR>Response.WriteFile(filepath & filename)<BR>.even if its just a piece of the puzzle :)...untested. Just munging what was there.<BR><BR><%@ Import Namespace="System.IO"%> <BR><script language="VB" runat="server"> <BR>Sub Page_Load(sender As Object, e As EventArgs) <BR><BR> Dim filename As String = trim(request.QueryString("file")) <BR> ' we don't need the directory for anything in here, <BR> ' so let's use this variable to get the FULL PATH to the file!<BR> Dim filepath As String = "C:Inetpubwebsites est_files\" & filename<BR><BR> ' might as well set up default content right here:<BR> Dim ContentType As String = "application/octet-stream" <BR><BR> Dim strFileType As String = lcase(Right(filename, 4)) <BR> Select Case strFileType <BR> Case ".asf" <BR> ContentType = "video/x-ms-asf" <BR> Case ".avi" <BR> ContentType = "video/avi" <BR> Case ".doc" <BR> ContentType = "application/msword" <BR> Case ".zip" <BR> ContentType = "application/zip" <BR> Case ".pdf" <BR> ContentType = "application/pdf" <BR> Case ".xls" <BR> ContentType = "application/vnd.ms-excel" <BR> Case ".gif" <BR> ContentType = "image/gif" <BR> Case ".jpg", "jpeg" <BR> ContentType = "image/jpeg" <BR> Case ".wav" <BR> ContentType = "audio/wav" <BR> Case ".mp3" <BR> ContentType = "audio/mpeg3" <BR> Case ".mpg", "mpeg" <BR> ContentType = "video/mpeg" <BR> Case ".rtf" <BR> ContentType = "application/rtf" <BR> Case ".htm", "html" <BR> ContentType = "text/html" <BR> Case ".asp" <BR> ContentType = "text/asp" <BR>' these lines not needed becuz default assigned initially<BR>' Case Else <BR>' 'Handle All Other Files <BR>' ContentType = "application/octet-stream" <BR> End Select <BR><BR> Response.Clear() <BR> Response.ContentType = ContentType<BR> Response.AddHeader("Content-Disposition", _ <BR> "attachment; filename=""" & filename & """") <BR> Response.Flush() <BR> Response.WriteFile(filepath) <BR><BR>End Sub <BR></script> <BR>...finger it out for his own self. But whuddahel, I changed my mind [??] since you were being so nice.<BR><BR>[You know where the word "nice" comes from? What it's Latin root is and the meaning thereof? You'll never want to be called "nice" again!]<BR><BR>
 
Back
Top