Solved

Help:  -- Dynamically transfering a file to the user's browser (ASP.NET VBScript)

Posted on 2004-10-08
7
475 Views
Last Modified: 2008-01-09
Hi.

I have written a .NET page that accepts the location of a file through the querystring, then passes it to the user's browser for download.  Currently however the browser always asks what I want to do with the file, and that some files are dangerous etc.  I would like the browser to simply process the file as it would normally (i.e. displaying .DOC's / .PDF's etc without prompting).
Can anyone recommend alternative code for achieving this??


Current code:
----------------
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>

<script runat="server">

Sub DownloadFile

            TRY
                  Dim strFileNamePath As String = Request.QueryString("file")
                  Dim myFile As FileInfo = New FileInfo(strFileNamePath)
                  Response.Clear()
                  Response.AddHeader("Content-Length", myFile.Length.ToString())
                  
                  SELECT CASE trim(lcase(Right(strFileNamePath, 4)))
                        Case ".doc"
                              Response.ContentType = "application/msword"
                        Case ".xls"
                              Response.ContentType = "application/vnd.ms-excel"
                        Case ".gif"
                              Response.ContentType = "image/gif"
                        Case ".jpg", "jpeg"
                              Response.ContentType = "image/jpeg"
                        Case ".bmp"
                              Response.ContentType = "image/bmp"
                        Case ".rtf"
                              Response.ContentType = "application/rtf"
                        Case ".htm", "html"
                              Response.ContentType = "text/html"
                        Case ".pdf"
                              Response.ContentType = "application/pdf"
                        Case ".ppt"
                              Response.ContentType = "application/mspowerpoint"
                        Case ".url"
                              Response.ContentType = "application/html"
                        Case Else
                              Response.ContentType = "application/octet-stream"
                  End Select
                  
                  Response.WriteFile(myFile.Fullname)
                  Response.End()
            
            CATCH
                  Response.Write("<html><head>")
                  Response.Write("<title>Newton Aycliffe Information Intranet</title>")
                  Response.Write("<link rel=""stylesheet"" type=""text/css"" href=""../style.css""></head>")
                  Response.Write("<body>")
                  Response.Write("<img src=""../images/icons/error.gif"" border=""0"" alt=""Error"" />")
                  Response.Write("Error: Cannot read file from server.  Please report this fault (including the desired file) to the IT Dept.")
                  Response.Write("</body></html>")
            END TRY            
End Sub

Sub Page_Load

      DownloadFile()

End Sub

</script>
0
Comment
Question by:Rouchie
  • 4
  • 3
7 Comments
 
LVL 14

Expert Comment

by:Thandava Vallepalli
Comment Utility
For that u require a client side ActiveX componetn.... using component u can download any files from internet to the client system..........

itvtk
0
 
LVL 25

Author Comment

by:Rouchie
Comment Utility
Is this correct?  Originally in classic ASP I wrote a similar page that achieved the result perfectly using ADODB.Stream.

This makes me wonder where the similar functionality is in ASP.NET to achieve the same result?  Surely it is unrealistic to expect all users to use an ActiveX component, especially in a time when browser security is so heavily emphasized!?
0
 
LVL 14

Expert Comment

by:Thandava Vallepalli
Comment Utility
ok.... understand.....

Now this functionality is disabled in IE.  Now u can't achive this by ASP or ASP.net

http://www.microsoft.com/downloads/details.aspx?FamilyId=FE2A5B1C-FF30-40A0-8E70-C9F1F4DCD8C2&displaylang=en

itsvtk
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 25

Author Comment

by:Rouchie
Comment Utility
I've got the code in ASP that works perfectly, even in the same browser that causes problems with the .NET version.  There must be an easy way to achieve this in .NET I would hope....

Classic ASP code========================
<%
' get the file to download
DIM fileToDownload
fileToDownload = Request.QueryString("file")

' start the loading procedure...
Const adTypeBinary = 1
Dim strFilePath

' Set filestream for appropriate file type
FileType = lcase(Right(fileToDownload, 4))
    Select Case FileType
        Case ".rtf"
            Response.ContentType = "application/rtf"
            Case ".htm", "html"
            Response.ContentType = "text/html"
            Case ".pdf"
                  Response.ContentType = "application/pdf"
            Case ".ppt"
                  Response.ContentType = "application/mspowerpoint"
        Case Else
            'Handle All Other Files
            Response.ContentType = "application/octet-stream"
    End Select

Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile fileToDownload

Response.BinaryWrite objStream.Read

objStream.Close
Set objStream = Nothing
Set FileType = Nothing
set fileToDownload = Nothing
%>
0
 
LVL 14

Accepted Solution

by:
Thandava Vallepalli earned 200 total points
Comment Utility
0
 
LVL 25

Author Comment

by:Rouchie
Comment Utility
Interesting URL, this is the one I started from before writing my own code...!  The code they provide throws a security error saying insufficient permissions to access the file, even though its in the IIS directory, so I can't really judge how well it actually works.  In the meantime, I've found an excellent alternative that does work at http://www.experts-exchange.com/Web/Q_20849510.html
The MIME type for any file can be found at filext.com to expand the code.

I'll leave the question open for a few days to allow other people to leave any suggestions, otherwise I've no objections to letting you have the points.
0
 
LVL 14

Expert Comment

by:Thandava Vallepalli
Comment Utility
ok... as u like i wish new suggesstions will be posted....

itsvtk
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now