Di bilah alamat browser, langsung masukkan jalur URL dari file DOC atau XLS atau JPG, dan file akan ditampilkan langsung di browser. Dalam banyak kasus, kami berharap dapat secara langsung memunculkan kotak prompt unduhan untuk diunduh pengguna. Apa yang harus kita lakukan? Berikut dua metode:
1. Atur IIS server Anda dan peta nama akhiran seperti DOC.
2. Atur tipe kontennya saat mengirim ke klien.
Metode 2 dijelaskan secara rinci di bawah ini
Kode Program:
<% Response.buffer = respons true.clear redup url redup fso, fl, flsize redup redm objstream, contentType, flname, isre, url1 '*************************************** file unduhan dilewati kapan panggilan DNAME = ("non)) '****************************************************************************************************************************** If Dname<>"" Then '*************************************************************** The server directory where the download file is stored is url=server.MapPath("/")&"/"&Dname '************************************************ End If Set fso=Server.CreateObject("Scripting.FileSystemObject") Set fl=fso.getfile(url) flsize=fl.size flName=fl.name Set fl=Nothing Set fso=Nothing %> <% Set objstream = server.createObject ("adodb.stream") objstream.open objstream.type = 1 objstream.loadFromFile url select case lcase (right (flname, 4)) case ".asf" contentType = "video/x-ms-asf" case ".Avi" contentpype = "content" content "content" case "case" case "content" content "content" content "content" content "content" content "content" content "content" content "content" content "content" content "content" content "content" content "content" content "content" content "content" content "content" ".zip" contentType = "aplikasi/zip" case ".xls" contentType = "application/vnd.ms-excel" case ".gif" contentType = "gambar/gif" case ".jpg", "jpeg" contentType = "gambar/jpeg" case ".Wav" contentype = "audio" caseo "cased" cased "case" case "case" case "case" content "case" content "case" case "content" case "case" case "case" "case" case "mpeg" contentType = "video/mpeg" case ".rtf" contentType = "application/rtf" case ".htm", "html" contentType = "Teks/html" case ".txt" contentType = "Text/Plain" case lain ContentType lain ContentType = "Octet" end "end" end "end" endtypose-d-disposition. FileName = "& flname response.addheader" panjang konten ", flSize response.charset =" utf-8 "response.contentType = contentType response.binarywrite objstream.read response.flush response.clear () objstream.close set objstream = nothing % %>Simpan item berikut sebagai download.asp dan Anda dapat menggunakan <aherf = "http://xxx.xxx.com/download.asp?n=file.doc"> Unduh! </a> Untuk mengunduh file.doc di direktori yang sama!
Tetapi ada masalah di sini bahwa tidak aman untuk secara langsung menulis jalur file.doc di URL, jadi solusinya adalah menyimpan jalur file.doc dalam database, dan mendapatkan jalur setelah mencari database
Jika Anda menambahkan penilaian di awal program ini:
jika instr (request.serverVariable ("http_referer"), "http: // nama domain Anda") = 0 lalu respons.end end if ifItu dapat mencegah orang lain mencuri hubungan mereka.