ASP如何实现文件上传啊???急急!!!!

theninthstar 2004-03-25 08:38:42
我的网站中的产品可以后台修改产品的图片,单是我如何通过ASP将图片上传的指定目录!!!谢谢了
...全文
24 点赞 收藏 10
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
aojiankd 2004-03-25
搜索"稻香老农"
回复
为何不用组件呢!
回复
gotocsdn123 2004-03-25
用组件ASPUPLOAD,速度狂快,用法简单!
回复
jerry7981 2004-03-25
然后把下面的语句存成upload_5xsoft.inc文件
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>

dim upfile_5xSoft_Stream

Class upload_5xSoft

dim Form,File,Version

Private Sub Class_Initialize
dim iStart,iFileNameStart,iFileNameEnd,iEnd,vbEnter,iFormStart,iFormEnd,theFile
dim strDiv,mFormName,mFormValue,mFileName,mFileSize,mFilePath,iDivLen,mStr
Version="化境编程界HTTP上传程序 Version 1.0"
if Request.TotalBytes<1 then Exit Sub
set Form=CreateObject("Scripting.Dictionary")
set File=CreateObject("Scripting.Dictionary")
set upfile_5xSoft_Stream=CreateObject("Adodb.Stream")
upfile_5xSoft_Stream.mode=3
upfile_5xSoft_Stream.type=1
upfile_5xSoft_Stream.open
upfile_5xSoft_Stream.write Request.BinaryRead(Request.TotalBytes)

vbEnter=Chr(13)&Chr(10)
iDivLen=inString(1,vbEnter)+1
strDiv=subString(1,iDivLen)
iFormStart=iDivLen
iFormEnd=inString(iformStart,strDiv)-1
while iFormStart < iFormEnd
iStart=inString(iFormStart,"name=""")
iEnd=inString(iStart+6,"""")
mFormName=subString(iStart+6,iEnd-iStart-6)
iFileNameStart=inString(iEnd+1,"filename=""")
if iFileNameStart>0 and iFileNameStart<iFormEnd then
iFileNameEnd=inString(iFileNameStart+10,"""")
mFileName=subString(iFileNameStart+10,iFileNameEnd-iFileNameStart-10)
iStart=inString(iFileNameEnd+1,vbEnter&vbEnter)
iEnd=inString(iStart+4,vbEnter&strDiv)
if iEnd>iStart then
mFileSize=iEnd-iStart-4
else
mFileSize=0
end if
set theFile=new FileInfo
theFile.FileName=getFileName(mFileName)
theFile.FilePath=getFilePath(mFileName)
theFile.FileSize=mFileSize
theFile.FileStart=iStart+4
theFile.FormName=FormName
file.add mFormName,theFile
else
iStart=inString(iEnd+1,vbEnter&vbEnter)
iEnd=inString(iStart+4,vbEnter&strDiv)

if iEnd>iStart then
mFormValue=subString(iStart+4,iEnd-iStart-4)
else
mFormValue=""
end if
form.Add mFormName,mFormValue
end if

iFormStart=iformEnd+iDivLen
iFormEnd=inString(iformStart,strDiv)-1
wend
End Sub

Private Function subString(theStart,theLen)
dim i,c,stemp
upfile_5xSoft_Stream.Position=theStart-1
stemp=""
for i=1 to theLen
if upfile_5xSoft_Stream.EOS then Exit for
c=ascB(upfile_5xSoft_Stream.Read(1))
If c > 127 Then
if upfile_5xSoft_Stream.EOS then Exit for
stemp=stemp&Chr(AscW(ChrB(AscB(upfile_5xSoft_Stream.Read(1)))&ChrB(c)))
i=i+1
else
stemp=stemp&Chr(c)
End If
Next
subString=stemp
End function

Private Function inString(theStart,varStr)
dim i,j,bt,theLen,str
InString=0
Str=toByte(varStr)
theLen=LenB(Str)
for i=theStart to upfile_5xSoft_Stream.Size-theLen
if i>upfile_5xSoft_Stream.size then exit Function
upfile_5xSoft_Stream.Position=i-1
if AscB(upfile_5xSoft_Stream.Read(1))=AscB(midB(Str,1)) then
InString=i
for j=2 to theLen
if upfile_5xSoft_Stream.EOS then
inString=0
Exit for
end if
if AscB(upfile_5xSoft_Stream.Read(1))<>AscB(MidB(Str,j,1)) then
InString=0
Exit For
end if
next
if InString<>0 then Exit Function
end if
next
End Function

Private Sub Class_Terminate
form.RemoveAll
file.RemoveAll
set form=nothing
set file=nothing
upfile_5xSoft_Stream.close
set upfile_5xSoft_Stream=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 toByte(Str)
dim i,iCode,c,iLow,iHigh
toByte=""
For i=1 To Len(Str)
c=mid(Str,i,1)
iCode =Asc(c)
If iCode<0 Then iCode = iCode + 65535
If iCode>255 Then
iLow = Left(Hex(Asc(c)),2)
iHigh =Right(Hex(Asc(c)),2)
toByte = toByte & chrB("&H"&iLow) & chrB("&H"&iHigh)
Else
toByte = toByte & chrB(AscB(c))
End If
Next
End function
End Class


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

Public function SaveAs(FullPath)
dim dr,ErrorChar,i
SaveAs=1
if trim(fullpath)="" or FileSize=0 or FileStart=0 or FileName="" then exit function
if FileStart=0 or right(fullpath,1)="/" then exit function
set dr=CreateObject("Adodb.Stream")
dr.Mode=3
dr.Type=1
dr.Open
upfile_5xSoft_Stream.position=FileStart-1
upfile_5xSoft_Stream.copyto dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=0
end function
End Class
</SCRIPT>
回复
tonync 2004-03-25
用 化境ASP无组件上传 V2.0
http://www.5xsoft.com/page/200311/56.htm
里面有示例
回复
jerry7981 2004-03-25
set upload=new upload_5xsoft
set file=upload.file("file")
upload.form("filepath")="file/"
formPath=upload.form("filepath")
if file.fileSize>0 then
file.saveAs Server.mappath(formPath&file.FileName)
filename=file.FileName
end if
回复
yangzixp 2004-03-25
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>

'''''''''''''''''''''''''''''''''''''''''''''''''
'
'请保留此信息: 稻香老农制作 http://www.5xSoft.com/
'
'''''''''''''''''''''''''''''''''''''''''''''''''

dim upfile_5xSoft_Stream

Class upload_5xSoft

dim Form,File,Version

Private Sub Class_Initialize
dim iStart,iFileNameStart,iFileNameEnd,iEnd,vbEnter,iFormStart,iFormEnd,theFile
dim strDiv,mFormName,mFormValue,mFileName,mFileSize,mFilePath,iDivLen,mStr
Version="化境编程界HTTP上传程序 Version 1.0"
if Request.TotalBytes<1 then Exit Sub
set Form=CreateObject("Scripting.Dictionary")
set File=CreateObject("Scripting.Dictionary")
set upfile_5xSoft_Stream=CreateObject("Adodb.Stream")
upfile_5xSoft_Stream.mode=3
upfile_5xSoft_Stream.type=1
upfile_5xSoft_Stream.open
upfile_5xSoft_Stream.write Request.BinaryRead(Request.TotalBytes)

vbEnter=Chr(13)&Chr(10)
iDivLen=inString(1,vbEnter)+1
strDiv=subString(1,iDivLen)
iFormStart=iDivLen
iFormEnd=inString(iformStart,strDiv)-1
while iFormStart < iFormEnd
iStart=inString(iFormStart,"name=""")
iEnd=inString(iStart+6,"""")
mFormName=subString(iStart+6,iEnd-iStart-6)
iFileNameStart=inString(iEnd+1,"filename=""")
if iFileNameStart>0 and iFileNameStart<iFormEnd then
iFileNameEnd=inString(iFileNameStart+10,"""")
mFileName=subString(iFileNameStart+10,iFileNameEnd-iFileNameStart-10)
iStart=inString(iFileNameEnd+1,vbEnter&vbEnter)
iEnd=inString(iStart+4,vbEnter&strDiv)
if iEnd>iStart then
mFileSize=iEnd-iStart-4
else
mFileSize=0
end if
set theFile=new FileInfo
theFile.FileName=getFileName(mFileName)
theFile.FilePath=getFilePath(mFileName)
theFile.FileSize=mFileSize
theFile.FileStart=iStart+4
theFile.FormName=FormName
file.add mFormName,theFile
else
iStart=inString(iEnd+1,vbEnter&vbEnter)
iEnd=inString(iStart+4,vbEnter&strDiv)

if iEnd>iStart then
mFormValue=subString(iStart+4,iEnd-iStart-4)
else
mFormValue=""
end if
form.Add mFormName,mFormValue
end if

iFormStart=iformEnd+iDivLen
iFormEnd=inString(iformStart,strDiv)-1
wend
End Sub

Private Function subString(theStart,theLen)
dim i,c,stemp
upfile_5xSoft_Stream.Position=theStart-1
stemp=""
for i=1 to theLen
if upfile_5xSoft_Stream.EOS then Exit for
c=ascB(upfile_5xSoft_Stream.Read(1))
If c > 127 Then
if upfile_5xSoft_Stream.EOS then Exit for
stemp=stemp&Chr(AscW(ChrB(AscB(upfile_5xSoft_Stream.Read(1)))&ChrB(c)))
i=i+1
else
stemp=stemp&Chr(c)
End If
Next
subString=stemp
End function

Private Function inString(theStart,varStr)
dim i,j,bt,theLen,str
InString=0
Str=toByte(varStr)
theLen=LenB(Str)
for i=theStart to upfile_5xSoft_Stream.Size-theLen
if i>upfile_5xSoft_Stream.size then exit Function
upfile_5xSoft_Stream.Position=i-1
if AscB(upfile_5xSoft_Stream.Read(1))=AscB(midB(Str,1)) then
InString=i
for j=2 to theLen
if upfile_5xSoft_Stream.EOS then
inString=0
Exit for
end if
if AscB(upfile_5xSoft_Stream.Read(1))<>AscB(MidB(Str,j,1)) then
InString=0
Exit For
end if
next
if InString<>0 then Exit Function
end if
next
End Function

Private Sub Class_Terminate
form.RemoveAll
file.RemoveAll
set form=nothing
set file=nothing
upfile_5xSoft_Stream.close
set upfile_5xSoft_Stream=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 toByte(Str)
dim i,iCode,c,iLow,iHigh
toByte=""
For i=1 To Len(Str)
c=mid(Str,i,1)
iCode =Asc(c)
If iCode<0 Then iCode = iCode + 65535
If iCode>255 Then
iLow = Left(Hex(Asc(c)),2)
iHigh =Right(Hex(Asc(c)),2)
toByte = toByte & chrB("&H"&iLow) & chrB("&H"&iHigh)
Else
toByte = toByte & chrB(AscB(c))
End If
Next
End function
End Class


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

Public function SaveAs(FullPath)
dim dr,ErrorChar,i
SaveAs=1
if trim(fullpath)="" or FileSize=0 or FileStart=0 or FileName="" then exit function
if FileStart=0 or right(fullpath,1)="/" then exit function
set dr=CreateObject("Adodb.Stream")
dr.Mode=3
dr.Type=1
dr.Open
upfile_5xSoft_Stream.position=FileStart-1
upfile_5xSoft_Stream.copyto dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=0
end function
End Class
</SCRIPT>
回复
yangzixp 2004-03-25
<% If Request.ServerVariables("Request_method") <> "POST" Then %>
<script language="javascript">
<!--
function checkinput()
{
if (news.newstitle.value=="")
{
alert("很抱歉,新闻标题不能为空,请重新填写")
document.news.newstitle.focus()
return false;
}
if (news.newstypes.value=="")
{
alert("很抱歉,请先选择新闻类别")
document.news.newstypes.focus()
return false;
}

if (news.newscontent.value=="")
{
alert("很抱歉,详细内容不能为空,请重新填写")
document.news.newscontent.focus()
return false;
}
return true;
}


function imageDelete() //删除所选择的图片
{
var cForm = document.news;
cForm.newspic.outerHTML='<input type=\'file\' name=\'newspic\' size=28 maxlength=10 style=\'border:1px double rgb(88,88,88);font:9pt\' onChange=\'pimg1LocalClick()\'>'
cForm.pimg.src = "../images/picdefault.gif";
}

function pimg1LocalClick() //选择的图片预览
{
var cForm = document.news;
document.news.pimg.src = document.news.newspic.value;
if ( document.news.newspic.value="")
{
cForm.pimg.src = "../images/picdefault.gif";
}
}

//-->
</script>
<form name="news" method="post" action="news_post.asp" enctype="multipart/form-data" onsubmit="return checkinput()">
<tr>
<td align="center">
<table width=100% border=0 align=center cellspacing="5" class="maintext">
<tr>
<td>
<table width=520 cellpadding=0 cellspacing=0 align="center" border=1 style="border-collapse: collapse" bordercolor=#333333>
<tr>
<td height=25 bgcolor=#dddddd colspan=3 class=hfont>   请在下面输入详细新闻信息,其中 <font color=red>*</font> 为必填内容
<tr><td height=25 style="letter-spacing:1px" width=100 align="center" bgcolor=#eeeeee class=hfont>新闻标题
<td width=420 colspan="2"> <input type=text size=50 maxlength=40 name="newstitle" style="border:1px double rgb(88,88,88);font:9pt"> <font color=red>*</font>
<tr><td height=25 style="letter-spacing:1px" width=100 align="center" bgcolor=#eeeeee class=hfont>新闻分类
<td width="318"> <select name="newstypes" style="border:1px double rgb(88,88,88);font:9pt">
<option value="">请选择...</option>
<option value="1">园区新闻</option>
<option value="2">社会新闻</option>
<option value="3">近期活动</option>
</select> <font color=red>*</font>
<td width="99" rowspan="3" align="center" valign="middle"><img src="../images/picdefault.gif" width=75 height=65 border=1 align="absmiddle" name="pimg" title="图片预览"><tr><td height=25 style="letter-spacing:1px" width=100 align="center" bgcolor=#eeeeee class=hfont>来源/作者
<td width="318"> <input type=text size=18 maxlength=10 name="newsauthor" style="border:1px double rgb(88,88,88);font:9pt">

<tr><td height=25 style="letter-spacing:1px" width=100 align="center" bgcolor=#eeeeee class=hfont>相关图片
<td width="318"> <input type=file size=28 maxlength=10 name="newspic" style="border:1px double rgb(88,88,88);font:9pt" onChange="pimg1LocalClick()"> <input type="button" value=" 去除 " style="border:1px double rgb(88,88,88);font:9pt" onClick="imageDelete()">

<tr>
<td colspan=3 align="center" valign="middle" height=150><textarea cols=69 rows=9 name="newscontent" style="border:1px double rgb(88,88,88);font:9pt"></textarea>


</table>
</td>
</tr>
<tr><td height=20 align="center"><input type="submit" name="submit" value=" 确定发布 " style="border:1px double rgb(88,88,88);font:9pt">   
<input type=reset value=" 重新填写 " style="border:1px double rgb(88,88,88);font:9pt">
</table></td>
</tr></form>
<%
else
response.write "<tr><td height=25 align='center'>"
dim upload,file,formName

dim newstitle,newstypes,newsauthor,newscontent

set upload=new upload_5xSoft ''建立上传对象
'开始获取文字信息
newstitle=upload.form("newstitle")
newstypes=upload.form("newstypes")
newsauthor=upload.form("newsauthor")
newscontent=htmlencode(upload.form("newscontent"))

'开始获取图片信息
set file=upload.file("newspic") ''生成一个文件对象
if file.FileSize>0 then ''如果 FileSize > 0 说明有文件数据
if right(file.filename,3)<>"gif" and right(file.filename,3)<>"GIF" and right(file.filename,3)<>"jpg" and right(file.filename,3)<>"JPG" and right(file.filename,3)<>"BMP" and right(file.filename,3)<>"bmp" then
response.write "抱歉,你上传图片的格式不对"
else
if file.FileSize>102400 then
response.write "抱歉,你上传的图片大小超过了500K"
else
dim filetypes,newfilename
filetypes="." & right(file.filename,3)
newfilename="../images/upload/" & year(now()) & month(now()) & day(now()) & hour(now()) & minute(now()) & second(now()) & filetypes
file.SaveAs Server.mappath(newfilename) ''保存文件
end if
end if
end if

'保存到数据库

sql="insert into jgnews(newstitle,newscontent,newstypes,viewtimes,newsauthor,ifpic,postuser,postip,posttime) values('"&newstitle&"', '"&newscontent&"', '"&newstypes&"', 0, '"&newsauthor&"', '"&newfilename&"', '"&useron&"', '"&Request.ServerVariables("REMOTE_ADDR")&"', '"&now()&"')"
conn.execute(sql)

set newstitle=nothing
set newstypes=nothing
set newsauthor=nothing
set newscontent=nothing
set file=nothing
set upload=nothing

response.write "<font color=red><br>祝贺,新闻发布成功!</font><br>"
end if
%>
回复
liaoxing 2004-03-25
网上很多这样的组件譬如:upload_5xsoft
回复
liaoxing 2004-03-25
用组件可以容易实现。
回复
相关推荐
发帖
ASP
创建于2007-09-28

2.8w+

社区成员

ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
申请成为版主
帖子事件
创建了帖子
2004-03-25 08:38
社区公告
暂无公告