본문 바로가기

Story/asp

asp file download

반응형
<% Option Explicit %>
<% Response.Buffer=True %>
<% 
   Public fso, objFile, fileName
   Public objStream, fileSize, filePath
   
   fileName=Request("image")
   filePath=Server.MapPath(fileName)
   
   Set fso=Server.CreateObject("Scripting.FileSystemObject")
   
   If Not(fso.FileExists(filePath)) Then
      Response.Write("<Font color=red>Image "&filePath&" does not exist!</Font>")
      Set fso=Nothing
      Response.END
   End If
   
   Set objFile=fso.GetFile(filePath)
   fileSize=objFile.Size
   
   Set objFile=Nothing
   Set fso=Nothing
   
   Set objStream=Server.CreateObject("ADODB.Stream")
   
   objStream.Open
   objStream.Type = 1 'adTypeBinary
   objStream.LoadFromFile filePath
   
   Response.AddHeader "Content-Disposition", "attachment; filename="&ExtractFileName(fileName)
   
   Response.ContentType = "Image/Jpeg"
   Response.AddHeader "Content-Length", fileSize
'   Response.Charset = "UTF-8"

   
   Response.BinaryWrite objStream.Read
   Response.Flush
   
   objStream.Close
   Set objStream = Nothing
   
   Function ExtractFileName(strFile)
      Dim arrTmp
      arrTmp=Split(strFile, "/")
      ExtractFileName=arrTmp(UBound(arrTmp))
   End Function
   
   'usage:
   '<A href="Image.asp?image=images/MyImage.jpg">Click to download</A>
%> 
 
파일이 존재하는지 여부를 먼저 체크해야함.
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists ("yourfile.txt") Then
    Response.Write "File exists"
Else
    Response.Write "File does not exist"
End If
 
 
 
http://www.plus2net.com/asp-tutorial/file-exist.php 여기에 있는내용을 기록해 두겠다.
 

Determining the existence of file or directory by using FileSystemObject in ASP

We can check whether file of folder exists at a location by using file system object ( FileSystemObject). This is often required to check the existence of a file or directory before using, otherwise system will generate an error message which is not good to display to the visitors. So before using any file or folder we can check the status of them.

FileSystemObject or fso uses physical path of the file or folder to check the existence so before using this we have to convert the virtual path to physical path for our folders or files by using server.mappath object.

We will use one variable to hold the instance of the FileSystemObject and using this we will check the existence of the file or folder.

Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

Next we will use the If Then Else statement to check the condition of the file or folder.

 

If objFSO.FolderExists(Server.MapPath("db")) Then
Response.Write "Folder Exists"
else
Response.Write "Folder does not exit"
End if
 

Here “db” is the name of the folder we are checking the existence. We have used the command Server.MapPath to change the virtual path to physical path of the folder db.

We can print the command based the response of the object ( True or False ) and display the message accordingly.

Same way we can use objFSO.FileExists to check the existence of a file at any location.

Dim FSOobj,FilePath
FilePath=Server.MapPath("text.txt") ' located in the same director
Set FSOobj = Server.CreateObject("Scripting.FileSystemObject")
if FSOobj.fileExists(FilePath) Then
Response.Write "File Exists "
Else
Response.Write "File does not exist"
End if
Set FSOobj = Nothing
 

Be the first to post comment on this article :

반응형

'Story > asp' 카테고리의 다른 글

SQL Server 암호화 알고리즘 정리  (0) 2015.03.16
Mssql To Xml  (0) 2013.03.13
asp c# .net 에서 mysql password 값을 만들기  (0) 2013.02.05
asp 로 rss feed (reader) 구현하기  (0) 2011.07.01
asp 쿠키(Cookies) 사용  (0) 2011.02.25