Welcome to WebmasterWorld Guest from

Forum Moderators: ocean10000

Message Too Old, No Replies

Upload an image using ASP

Searched, and can't find any "examples"

2:23 am on Mar 8, 2006 (gmt 0)

Preferred Member

10+ Year Member

joined:Dec 3, 2004
votes: 0

Hello out there. I am "revising" a current page on our company site, and I need a script added for employees to upload images to a certain directory.

I have found many "pre built" scripts, but have failed at incorperating any of them into the current design. And I fear without hours and hours of filtering through the hundereds of lines of code writen by someone else, I won't be able to get this done.

What I am looking for is a starting point to learn about writing the script myself, or get a SIMPLE example of an upload script. I had a pretty well put together script written in PHP for our other site ... But this one is already written in ASP. It has to be simpler than 287 lines of code doesn't it?

I don't need anyone elses error handling etc. I can write my own. I just need to the get the file uploaded. Well over half of the lines in these scripts I've found online are just a ton of error handling. I can do without that. Just need the upload script.

Anyone have a tutorial or a list of globals and calls I can look into? Or even a small few-liner without all the checks etc?


-- Zak

3:03 am on Mar 8, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 9, 2006
votes: 0

If you just want to upload an image file and not perform any manipulation on it, then try searching google for:
ASP File Upload
11:09 pm on Mar 8, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 13, 2002
votes: 0

Why not use a component to do the upload, the scripts are quite slow for large files.

With a component you can implement you own error handling and do the upload in a few lines of code.

3:06 am on Mar 9, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 12, 2006
votes: 0

Good advice has been given so far. I've used the ASPUpload component for years to upload images, and I've never had problems with it.

The PHP code needed to upload images is much simpler than its ASP counterpart!

12:43 am on Mar 11, 2006 (gmt 0)

Preferred Member

10+ Year Member

joined:Dec 3, 2004
votes: 0

I finally ended up using AspUpload. I took the time to custimize it. It proved more difficult to find a pure example of "how to". It's working great. Thanks for your replies!

-- Zak

1:43 pm on Apr 14, 2006 (gmt 0)

Full Member

10+ Year Member

joined:Aug 16, 2004
votes: 0

I too use ASPUpload from Persits, with pretty heavy duty requirements in terms of file size, number of files, and number of concurrent users uploading.

It works great. For a free, smaller-scale (I wouldn't trust it on a busy site) option, type in 'Lewis Moton ASP Upload' in Google. It is a free, pure ASP upload, that is pretty well done.

2:20 pm on Apr 16, 2006 (gmt 0)

New User

10+ Year Member

joined:Apr 16, 2006
votes: 0


'FileName "doc.txt"
'FileSize 1210 (kb)
'FileType "text/plain"
'FileExt ".txt"
'FilePath "c:\myfile"

dim oUpFileStream
Class upload_file
dim Form,File,Version
Private Sub Class_Initialize
dim RequestBinDate,sStart,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo
dim iFileSize,sFilePath,sFileType,sFormvalue,sFileName
dim iFindStart,iFindEnd
dim iFormStart,iFormEnd,sFormName
Version="无组件上传类 Version 1.00"
set Form = Server.CreateObject("Scripting.Dictionary")
set File = Server.CreateObject("Scripting.Dictionary")
if Request.TotalBytes < 1 then Exit Sub
set tStream = Server.CreateObject("adodb.stream")
set oUpFileStream = Server.CreateObject("adodb.stream")
oUpFileStream.Type = 1
oUpFileStream.Mode = 3
oUpFileStream.Write Request.BinaryRead(Request.TotalBytes)
RequestBinDate = oUpFileStream.Read
iFormEnd = oUpFileStream.Size
bCrLf = chrB(13) & chrB(10)
sStart = MidB(RequestBinDate,1, InStrB(1,RequestBinDate,bCrLf)-1)
iStart = LenB (sStart)
iFormStart = iStart+2
iInfoEnd = InStrB(iFormStart,RequestBinDate,bCrLf & bCrLf)+3
tStream.Type = 1
tStream.Mode = 3
oUpFileStream.Position = iFormStart
oUpFileStream.CopyTo tStream,iInfoEnd-iFormStart
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sInfo = tStream.ReadText
iFormStart = InStrB(iInfoEnd,RequestBinDate,sStart)-1
iFindStart = InStr(22,sInfo,"name=""",1)+6
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFormName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
if InStr (45,sInfo,"filename=""",1) > 0 then
set oFileInfo= new FileInfo
iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
oFileInfo.FileName = GetFileName(sFileName)
oFileInfo.FilePath = GetFilePath(sFileName)
oFileInfo.FileExt = GetFileExt(sFileName)
iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14
iFindEnd = InStr(iFindStart,sInfo,vbCr)
oFileInfo.FileType = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
oFileInfo.FileStart = iInfoEnd
oFileInfo.FileSize = iFormStart -iInfoEnd -2
oFileInfo.FormName = sFormName
file.add sFormName,oFileInfo
tStream.Type = 1
tStream.Mode = 3
oUpFileStream.Position = iInfoEnd
oUpFileStream.CopyTo tStream,iFormStart-iInfoEnd-2
tStream.Position = 0
tStream.Type = 2
tStream.Charset = "gb2312"
sFormvalue = tStream.ReadText
form.Add sFormName,sFormvalue
end if
iFormStart = iFormStart+iStart+2
loop until (iFormStart+2) = iFormEnd
set tStream = nothing
End Sub

Private Sub Class_Terminate
if not Request.TotalBytes<1 then
set oUpFileStream =nothing
end if
set Form=nothing
set File=nothing
End Sub

Private function GetFilePath(FullPath)
If FullPath <> "" Then
GetFilePath = left(FullPath,InStrRev(FullPath, "\"))
GetFilePath = ""
End If
End function

Private function GetFileName(FullPath)
If FullPath <> "" Then
GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)
GetFileName = ""
End If
End function

Private function GetFileExt(FullPath)
If FullPath <> "" Then
GetFileExt = mid(FullPath,InStrRev(FullPath, "."))
GetFileExt = ""
End If
End function
End Class

Class FileInfo
dim FormName,FileName,FilePath,FileSize,FileType,FileStart,FileExt
Private Sub Class_Initialize
FileName = ""
FilePath = ""
FileSize = 0
FileStart= 0
FormName = ""
FileType = ""
FileExt = ""
End Sub

Public function SaveToFile(FullPath)
dim oFileStream,ErrorChar,i
if trim(fullpath)="" or right(fullpath,1)="/" then exit function
set oFileStream=CreateObject("Adodb.Stream")
oUpFileStream.copyto oFileStream,FileSize
oFileStream.SaveToFile FullPath,2
set oFileStream=nothing
end function
End Class

the Form

<form name="form" method="post" action="upfile.asp" enctype="multipart/form-data" onsubmit="return check();">
<input type="hidden" name="act" value="upload">
<input type="hidden" name="upcount" value="1">
<input type="hidden" name="filepath" value="images/">
name:<input type="text" name="picname" id="picname"><br/>
<textarea cols="12" rows="8" name="description"></textarea>
<input type="file" name="file1" value="" width="12px" size="12" id="file1"><br/>
<input type="submit" name="Submit" value=" submit" > <input type="reset" value=" reset" >


<!--#include file="conn.asp"-->
<!--#include FILE="upclass.inc"-->
<style type="text/css">
body{font-size:12px;LINE-HEIGHT: 1.5em}
a { color: #000000; text-decoration: underline}
a:hover { text-decoration: underline}
<body bgcolor="#D7D7C3">
<br>图片上传结果<hr size=1 noshadow width=150 align=left><br>
dim upload,file,formName,formPath,iCount,filename
set upload=new upload_file ''建立上传对象
if upload.form("picname")="" then ''
HtmEnd "未输入图像名称!"
set upload=nothing
if right(formPath,1)<>"/" then formPath=formPath&"/"
end if
for each formName in upload.file ''列出所有上传了的文件
set file=upload.file(formName) ''生成一个文件对象
if LCase(fileExt)<>".gif" and LCase(fileExt)<>".jpg" and LCase(fileExt)<>".bmp" and LCase(fileExt)<>".png" then
response.write "<br>"&"文件不是图片[<a href=""javascript:history.back();"">返回</a>]<br><p>允许的格式为:gif;jpg;bmp;png!</p><p>A_Nice_Idea</p></body></html>"
end if
if file.FileSize>0 then ''如果 FileSize > 0 说明有文件数据
file.Savetofile Server.mappath(formPath&filename) ''保存文件
response.write file.FilePath&file.FileName&" ("&file.FileSize&"kb)"&" 上传成功!<br>"
end if
set rs2=server.createobject("adodb.recordset")
insertsql="select * from views"
rs2.open insertsql,db,1,3
set rs2=nothing
set file=nothing
set upload=nothing ''删除此对象
Htmend "文件上传成功!"
sub HtmEnd(Msg)
set upload=nothing
response.write "<br>"&Msg&" <br/>[<a href=""javascript:history.back();"">返回</a>]<br><p>感谢您的参与,上传成功的图片通过审核后将在本站展出。A_Nice_Idea</p></body></html>"
end sub

These works well,for small files
the remarks are Chinese