Toon posts:

[ASP] forceren downloaden bestanden wisselvallig

Pagina: 1
Acties:

Verwijderd

Topicstarter
Ik ben bezig met het maken van een script waarmee bezoekers van ons intranet documenten e.d. automatisch kunnen downloaden. Het werkt prima wanneer het bijvoorbeeld een asp file is, echter wanneer het word of excell betreft wordt het betreffende bestand gewoon geopend in de browser. Wat zie ik over het hoofd? De search maakte me weinig wijzer...

filedownload.asp:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<%
Response.Expires = 0

Dim objXMLHTTP
Dim objXml
Dim vFileName
Dim strFileReq
Dim strFileType
Dim strContentType

strFileReq = Request.QueryString("req")

If strFileReq = "" Then
%>
<html>
<head>
    <title>Download a file</title>
</head>
<body>
    <form method="get" action="<%= Request.ServerVariables("SCRIPT_NAME") %>">
    <span style=" font-family:Verdana; font-size:12px;text-decoration:underline;">Enter the URL to the file you want download.</span><br/><br/>
    <input type="text" size="30" name="req" value="" style="height:20px;border: black 1px solid; font-family: Verdana;">
    <input type="submit" value="Retreive file" style="height:20px;font-family: Verdana; background-color: white; border: black 1px solid; font-weight:bold;">
    </form>
</body>
</html>
<%
Else
    On Error Resume Next
    Set objXml = Server.CreateObject("Microsoft.XMLHTTP")
    objXml.Open "GET", strFileReq, False
    objXml.Send

    If InStr(strFileReq,"/") <> 0 Then
        vFileName = Split(strFileReq,"/")
        strFileReq = Lcase(vFileName(UBound(vFileName)))
    End If

    Response.AddHeader "content-disposition","attachement;filename=" & strFileReq

    If InStr(strFileReq,".") <> 0 Then
        vFileName = Split(strFileReq,".")
        strFileType = Lcase(vFileName(UBound(vFileName)))
    End If
    
    Select Case strFileType
        Case "asf"
            strContentType = "video/x-ms-asf"
        Case "avi"
            strContentType = "video/avi"
        Case "doc"
            strContentType = "application/vnd.ms-word"
        Case "zip"
            strContentType = "application/zip"
        Case "xls"
            strContentType = "application/vnd.ms-excel"
        Case "gif"
            strContentType = "image/gif"
        Case "jpg", "jpeg"
            strContentType = "image/jpeg"
        Case "wav"
            strContentType = "audio/wav"
        Case "mp3"
            strContentType = "audio/mpeg3"
        Case "mpg", "mpeg"
            strContentType = "video/mpeg"
        Case "rtf"
            strContentType = "application/rtf"
        Case "htm", "html"
            ContentType = "text/html"
        Case "asp"
            strContentType = "text/asp"
        Case Else
            strContentType = "application/octet-stream"
    End Select

    Response.Clear 
    Response.ContentType = strContentType
    Response.BinaryWrite objXml.ResponseBody

    Set objXml = Nothing
End If
%>

Verwijderd

Je moet het mimetype niet als 'word' of 'excel' zetten wanneer het een worddoc is. Internet Explorer ziet dan niet dat het een word / excel doc is en zal het standaard 'wilt u downloaden' schermpje laten zien.

Verwijderd

Topicstarter
ik heb de cases "doc" en "xls" uitgezet zodat als strContentType "application/octet-stream" wordt gepakt en nog wordt een worddocument geopend. :?

  • Basszje
  • Registratie: Augustus 2000
  • Laatst online: 21:24

Basszje

Reisvaap!]

Schijnt idd niet altijd even goed te werken. Is geloof ik ook een bekende bug in explorer.

Beware of listening to the imposter; you are undone if you once forget that the fruits of the earth belong to us all, and the earth itself to nobody.


  • jochemd
  • Registratie: November 2000
  • Laatst online: 31-08 19:19
De filename in de content-disposition header moet tussen quotes. Verder doe je alles goed, maar houden bepaalde browsers (voornamelijk IE) zich niet aan de suggesties uit sectie 19.5.1 van RFC 2616.

Als ik jou was zou ik gaan klagen bij de fabrikanten van de desbetreffende browsers.

Verwijderd

Topicstarter
da's minder...

Ben ff gaan zoeken naar die bug waar je het over had, en vond deze link bij MS. Onderaan staat ook nog deze opmerking:
You might think that setting the Content-Type to a type unknown to Explorer would force the File Download dialog box. In fact, the Explorer "sniffs" the data sent from the server and detects the type of file being sent. If it recognizes the file type, it will behave according to the user preference. At this point, there is no completely reliable way to force the File Download dialog box to appear when downloading a file to Internet Explorer.
:'(
Pagina: 1