관리 메뉴

nalaolla

asp파일 강제 다운로드 본문

ASP

asp파일 강제 다운로드

날아올라↗↗ 2015. 12. 10. 13:43
728x90


웹상에서 일부파일들(txt, 이미지, pdf)파일의 경우 브라우져에서 바로 열리기 때문에 별도의 다운로드 기능을 가진 페이지가 필요하다..


1. 다운로드 link페이지


link.asp


<a href="download.asp?fn=iisstart.htm&ofn=abc.htm">download</a>




2. 실제 다운로드를 강제할 페이지


<% @ codepage="65001" language="VBScript" %>

<%

'일부 웹에서 열리는 파일(txt, 이미지)을 강제로 다운로드 하기위함.

 session.codePage = 65001

 Response.CharSet = "UTF-8"

%>

<meta content="text/html;charset=utf-8" http-equiv="content-type" />

<%

Response.Expires = -10000

Server.ScriptTimeOut = 300


' 파라미터값 처리

Dim fn : fn = Trim(Request("fn"))   '실제파일이 있는경로명+파일명    /data/xml/abc.xml

Dim ofn : ofn = Trim(Request("ofn"))  '다운로드시 변경되는 파일명     abcd.xml --> abc.xml이 abcd.xml로 파일명변경되어 다운로드 된다.'

' 확장자 추출

Dim TmpStr : TmpStr = Split(Filename, ".")

Dim Ext : Ext = ""

Dim Name


For Each Name In TmpStr

Ext = Name

Next


Ext = LCase(Ext)


ofn = Server.UrlEncode(ofn)


ofn = Replace(ofn, "+", "")

ofn = Replace(ofn, "%2E" & Ext, "." & Ext)



Dim FullPath : FullPath = server.mappath("/")


Dim nFullPath : nFullPath = ""

Dim FileInfo : FileInfo = Split(fn, "/")

Dim FilePath : FilePath = FullPath

Dim FileName : FileName = ""


For i = 1 To Int(UBound(FileInfo)) - 1

FilePath = FilePath &"\"& FileInfo(i)

Next

FilePath = Replace(FilePath, "\\", "\")

FileName = FileInfo(UBound(FileInfo))

nFullPath = FilePath &"\"& FileName



Response.ContentType = "application/octet-stream"

Response.AddHeader "Content-Disposition", "attachment;filename="&ofn &""

Response.AddHeader "Content-Type", "text/html; charset=utf-8"

Response.AddHeader "Content-Transfer-Encoding", "binary"

Response.AddHeader "Pragma", "no-cache"

Response.AddHeader "Expires", "0" 



' 스트림을 선언


Set objStream = Server.CreateObject("ADODB.Stream")

objStream.Open


objStream.Type = 1

objStream.LoadFromFile nFullPath '다운받을경로(파일명포함)

strFile = objStream.Read



Response.BinaryWrite strFile

Set objStream = Nothing 

%>

728x90