Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-10-08
7
Medium Priority
?
485 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 800 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
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 video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Integration Management Part 2
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

885 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