Solved

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

Posted on 2004-10-08
7
478 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp syntax 3 58
ASP/VB email question 4 46
Syntax for query to update table 2 30
Adjust an existing ASP Query 45 17
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
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/…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 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