Solved

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

Posted on 2004-10-08
7
481 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

622 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