2014-09-12 3 views
2

버튼 클릭으로 엑셀 파일 (서버에 저장)을 다운로드하려고합니다. 이벤트는 빈 대신 "WORK.xls"엑셀 파일 클래식 ASP 다운로드

<% 
    strFile = "WORK.xls" 

    Response.ContentType = "application/octet-stream" 
    Response.AddHeader "Content-Disposition", "attachment; filename=" & strFile 

    set app = Server.CreateObject("ADODB.Stream") 
    app.open 
    app.type = adTypeBinary 
    app.LoadFromFile(Server.MapPath("WORK.xls")) 

    response.binarywrite app.Read 

    app.close 

    Set app = nothing 

%> 
+0

당신은 문제가 무엇을하는 데 문제가 있습니까? –

+0

"WORK.xls"서버에 저장된 파일 대신 빈 엑셀 파일 다운로드 – Sash

답변

1

의 파일을 엑셀 다운로드 기능은 발생하면이 시도 :

DownloadFile "WORK.xls" 

Private Sub DownloadFile(file) 
    '--declare variables 
    Dim strAbsFile 
    Dim strFileExtension 
    Dim objFSO 
    Dim objFile 
    Dim objStream 
    '-- set absolute file location 
    strAbsFile = Server.MapPath(file) 
    '-- create FSO object to check if file exists and get properties 
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 
    '-- check to see if the file exists 
    If objFSO.FileExists(strAbsFile) Then 
     Set objFile = objFSO.GetFile(strAbsFile) 
     '-- first clear the response, and then set the appropriate headers 
     Response.Clear 
     '-- the filename you give it will be the one that is shown 
     ' to the users by default when they save 
     Response.AddHeader "Content-Disposition", "attachment; filename=" & objFile.Name 
     Response.AddHeader "Content-Length", objFile.Size 
     Response.ContentType = "application/octet-stream" 
     Set objStream = Server.CreateObject("ADODB.Stream") 
     objStream.Open 
     '-- set as binary 
     objStream.Type = 1 
     Response.CharSet = "UTF-8" 
     '-- load into the stream the file 
     objStream.LoadFromFile(strAbsFile) 
     '-- send the stream in the response 
     Response.BinaryWrite(objStream.Read) 
     objStream.Close 
     Set objStream = Nothing 
     Set objFile = Nothing 
    Else 'objFSO.FileExists(strAbsFile) 
     Response.Clear 
     Response.Write("No such file exists.") 
    End If 
    Set objFSO = Nothing 
End Sub 

링크 : http://www.evagoras.com/2011/02/08/downloading-any-file-using-asp-fso-and-the-adodb-stream-object/

+1

고맙습니다! – Sash