웹 사이트를 방문 할 때 좌절하고 문서, 이미지 및 기타 콘텐츠를 다운로드 할 수 있으며 다운로드를 원하십니까? 대신 컨텐트 항목을 클릭하면 브라우저에서 계속 열립니다. 의료 회사의 프로젝트 에서이 문제에 반대하여 PDF 파일을 다운로드하여 열지 않아야했습니다. 다른 이름으로 저장 대화 상자를 여는 방법이 있습니다.

이 학습서에서는 파일이 데이터베이스가 아닌 서버에 로컬로 저장되어 있다고 가정합니다. (그것은 완전히 다른 토론입니다)

먼저 파일 위치를 알아야합니다. 파일이 //yourserver.com/files/filename.ext에 있다고 가정하겠습니다. Server.MapPath가 "/ files"라고 가정합니다.

이제이 시점에서 파일 다운로드를 제어 할 ASP 파일을 만들어 보겠습니다. 메모장이나 자주 사용하는 HTML 편집기를 사용하여 비어있는 새 문서를 만들어 시작하십시오. 다음 ASP 코드를 배치하십시오.

<%
'=======================
'Define the names of your functions
'=======================
Dim Stream
Dim Contents
Dim FileName
Dim FileExt
Const adTypeBinary = 1
'=======================
'Get the actual file name from the URL that is passed to the browser
'=======================
FileName = request.querystring("filename") 'Get the name from the URL
'=======================
'GIVE AN ERROR MESSAGE IF THE URL IS EMPTY
'=======================
if FileName = "" Then
response.write "Filename Not specified."
response.end
end if
'=======================
'prevent access to certain files
'=======================
FileExt = Mid(FileName, InStrRev(FileName, ".") + 1)
select case UCase(FileExt)
Case "ASP", "ASA", "ASPX", "ASAX", "MDB"
response.write "You cannot access these file types."
response.end
end select
'=======================
'Start the download process if all is good
'=======================
response.clear
response.contentType = "application/octet-stream"
response.addheader "content-disposition", "attachment; filename=" & FileName
set stream = server.CreateObject("ADODB.Stream")
stream.type = adTypeBinary
stream.open
stream.LoadFromFile Server.MapPath("/files") & FileName
while not stream.EOS
response.BinaryWrite Stream.Read(1024 * 64)
wend
stream.Close
Set stream = Nothing
response.Flush
response.End
%>


그게 다야. 이제이 파일을 저장하고 서버에 업로드하고 파일 이름을 filedownloader.asp로 지정하십시오.

이제 서버의 파일 이름이 familytree.pdf라고 가정하겠습니다.

그런 다음 브라우저에서 다음 URL을 전달하면 열기로 파일 저장 대화 상자가 나타납니다.

//yourserver.com/filedownloader.asp?filename=familytree.pdf

그게 다야! 그러면 이것을 볼 수 있습니다 :



운이 좋으면 데이터베이스 및 기타 멋진 기능을 사용하여 사용자가 액세스하려는 파일에 현재 웹 브라우저에서 다운로드 할 수있는 기능을 제공하여 웹 사이트에 "숙박"할 수 있도록하여이를 향상시킬 수 있습니다. .

비디오 지침: Technology Stacks - Computer Science for Business Leaders 2016 (할 수있다 2024).