homepage Welcome to WebmasterWorld Guest from 54.211.157.103
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
Forum Library, Charter, Moderators: ocean10000

Microsoft IIS Web Server and ASP.NET Forum

    
Upload an image using ASP
Searched, and can't find any "examples"
lZakl




msg:949891
 2:23 am on Mar 8, 2006 (gmt 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?

Thanks!

-- Zak

 

leapforward




msg:949892
 3:03 am on Mar 8, 2006 (gmt 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

aspdaddy




msg:949893
 11:09 pm on Mar 8, 2006 (gmt 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.

celgins




msg:949894
 3:06 am on Mar 9, 2006 (gmt 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!

lZakl




msg:949895
 12:43 am on Mar 11, 2006 (gmt 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

flyerguy




msg:949896
 1:43 pm on Apr 14, 2006 (gmt 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.

picassoxw




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

upclass.inc

<%
'--------------------------------------------------------------------
'File:c:\myfile\doc.txt
'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.Open
oUpFileStream.Write Request.BinaryRead(Request.TotalBytes)
oUpFileStream.Position=0
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
'分解项目
Do
iInfoEnd = InStrB(iFormStart,RequestBinDate,bCrLf & bCrLf)+3
tStream.Type = 1
tStream.Mode = 3
tStream.Open
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
else
'如果是表单项目
tStream.Close
tStream.Type = 1
tStream.Mode = 3
tStream.Open
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
tStream.Close
iFormStart = iFormStart+iStart+2
'如果到文件尾了就退出
loop until (iFormStart+2) = iFormEnd
RequestBinDate=""
set tStream = nothing
End Sub

Private Sub Class_Terminate
'清除变量及对像
if not Request.TotalBytes<1 then
oUpFileStream.Close
set oUpFileStream =nothing
end if
Form.RemoveAll
File.RemoveAll
set Form=nothing
set File=nothing
End Sub

'取得文件路径
Private function GetFilePath(FullPath)
If FullPath <> "" Then
GetFilePath = left(FullPath,InStrRev(FullPath, "\"))
Else
GetFilePath = ""
End If
End function

'取得文件名
Private function GetFileName(FullPath)
If FullPath <> "" Then
GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)
Else
GetFileName = ""
End If
End function

'取得扩展名
Private function GetFileExt(FullPath)
If FullPath <> "" Then
GetFileExt = mid(FullPath,InStrRev(FullPath, "."))
Else
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
SaveToFile=1
if trim(fullpath)="" or right(fullpath,1)="/" then exit function
set oFileStream=CreateObject("Adodb.Stream")
oFileStream.Type=1
oFileStream.Mode=3
oFileStream.Open
oUpFileStream.position=FileStart
oUpFileStream.copyto oFileStream,FileSize
oFileStream.SaveToFile FullPath,2
oFileStream.Close
set oFileStream=nothing
SaveToFile=0
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/>
<br/>
<textarea cols="12" rows="8" name="description"></textarea>
<br/>
File:<br/>
<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" >
</form>

upfile.asp

<!--#include file="conn.asp"-->
<!--#include FILE="upclass.inc"-->
<html>
<head>
<title></title>
<style type="text/css">
body{font-size:12px;LINE-HEIGHT: 1.5em}
a { color: #000000; text-decoration: underline}
a:hover { text-decoration: underline}
</style>
</head>
<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
response.end
else
formPath=upload.form("filepath")
''在目录后加(/)
if right(formPath,1)<>"/" then formPath=formPath&"/"
end if
iCount=0
for each formName in upload.file ''列出所有上传了的文件
set file=upload.file(formName) ''生成一个文件对象
'开始检查后缀
fileExt=file.fileExt
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>"
response.end
end if
'后缀检查结束
if file.FileSize>0 then ''如果 FileSize > 0 说明有文件数据
filename=year(now())&month(now())&day(now())&hour(now())&minute(now())&second(now())&"nkplant"&fileExt
file.Savetofile Server.mappath(formPath&filename) ''保存文件
response.write file.FilePath&file.FileName&" ("&file.FileSize&"kb)"&" 上传成功!<br>"
iCount=iCount+1
end if
'开始写入数据库
set rs2=server.createobject("adodb.recordset")
insertsql="select * from views"
rs2.open insertsql,db,1,3
rs2.addnew
rs2("url")=filename
rs2("picname")=trim(upload.form("picname"))
rs2("description")=trim(upload.form("description"))
rs2.update
set rs2=nothing
db.close
'写入数据库结束
set file=nothing
next
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>"
response.end
end sub
%>
</body>
</html>

These works well,for small files
the remarks are Chinese

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved