Solved

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

Posted on 2004-10-08
7
476 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
ID: 12257004
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
ID: 12257018
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
ID: 12257060
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 25

Author Comment

by:Rouchie
ID: 12257196
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
ID: 12257453
0
 
LVL 25

Author Comment

by:Rouchie
ID: 12257532
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
ID: 12257592
ok... as u like i wish new suggesstions will be posted....

itsvtk
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Syntax Help on SP 4 56
Classic ASP + JS 4 79
UTC (timezone) without using an API 16 39
Auto Submit on dropdown box 14 51
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

19 Experts available now in Live!

Get 1:1 Help Now