ASP中如何实现文件上传及把本地的图片存入数据库中.

husz2001 2002-12-28 10:34:30
1.如何实现把某个本地文件上传至服务器的某个目录,并把相应的链接记入数据表中.
2.同时还要实现把本本地的图片保存入表.(说明:这里的数据库限定在SQL SERVER).
请高手指点迷津.急用.解答正确给200分.给出详细实现过程,给500分.
这是我第一次提问题,有哪位古道热肠的朋友能抽出宝贵时间帮一下忙.
这里先行谢过.
...全文
56 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
husz2001 2003-01-02
  • 打赏
  • 举报
回复
结贴,感谢大家的帮助.我用了另外一种方法来实现.不过大家的我也都测试过了.
分还是要给的.
江南昆虫 2002-12-28
  • 打赏
  • 举报
回复
用上传组件吧,这里有详细的说明和范例,完全能满足你的需要:)


下载地址1:http://www.aspsky.net/download/old/lyfupload12b.zip

软件简介:

LyfUpload 是一个免费 的ASP组件,遵从 RFC-1867 HTTP 请求, 它可以在ASP页面中 接收客户端浏览器使用
encType= "multipart/form-data" 的Form上载的文件。 本版本支持单文件上载、多文件上载、限制文件大小
上载、限制某一类型文件上载、 文件上载到数据库、数据库中读取文件及文件上载重命名等功能。


gaopq_11 2002-12-28
  • 打赏
  • 举报
回复
我自己用的是chinasp!
<form method="POST" enctype="multipart/form-data" action="savemanage2.asp" name="fileup">
<input type="hidden" name="CopyrightInfo" value="http://www.chinaasp.com">
...
</form>

--------------------------处理附件----------------------------
dim fs,fiel,fj,r_fj_name
set f=FileUp.files("acc_file")
if f.isempty=false then
fileN=f.filename
r_fj_name=fileN
f.saveas server.MapPath("upfile/")&"\"&fileN
fj=1
else
fj="0"
fileN="*"
end if
theghost 2002-12-28
  • 打赏
  • 举报
回复
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="照片上传"
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>
theghost 2002-12-28
  • 打赏
  • 举报
回复
例子:

第一个页面:

<table width="784" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td align="center" valign="top" bgcolor="#FFFFFF"> <br> <font color="#CC6600"><br>
<br>
</font> <br> <br> <form name="form1" method="post" action="upfile.asp" enctype="multipart/form-data" >
<table width="70%" border="0" cellpadding="5" cellspacing="2">
<tr>
<td width="16%" height="55" align="right" bgcolor="#FFFFDF" class="main1"><font color="#CC6600">图片:</font></td>
<td align="left" bgcolor="#FAEEC0" class="main1"><input id=ss name="ss" type="file" class="input" style="width:300" value="">
</td>
</tr>
<tr>
<td width="16%" height="55" align="right" bgcolor="#FFFFDF" class="main1"><font color="#CC6600">id:</font></td>
<td align="left" bgcolor="#FAEEC0" class="main1"><input name="id" type="text" class="input" style="width:300" value="">
</td>
</tr>
<tr>
<td height="55" align="right" bgcolor="#FFFFDF" class="main1"><font color="#CC6600">路径:</font></td>
<td align="left" bgcolor="#FAEEC0" class="main1"><input name="filepath" type="text" ></td>
</tr>
<tr align="center">
<td height="50" colspan="2" class="main1"> <input type=submit value="添加" name=Submit2 >
</td>
</tr>
</table>


upfile.asp

<!--#include FILE="upload_5xsoft.inc"-->
<!--#include file="../conn.asp"-->
<html>
<head>
<title>文件上传</title>
</head>
<body>
<br>地图上传<br><hr size=1 noshadow width=300 align=left><br><br>
<%


function makefilename(ext)
fname = now()
fname = replace(fname,"-","")
fname = replace(fname," ","")
fname = replace(fname,":","")
fname = replace(fname,"PM","")
fname = replace(fname,"AM","")
fname = replace(fname,"上午","")
fname = replace(fname,"下午","")
makefilename=fname & ext
end function

dim upload,file,formName,formPath,iCount
set upload=new upload_5xSoft ''建立上传对象

'response.write upload.Version&"<br><br>" ''显示上传类的版本

if upload.form("filepath")="" then ''得到上传目录
HtmEnd "请输入要上传至的目录!"
set upload=nothing
response.end
else
formPath="../hz/"&upload.form("filepath")
''在目录后加(/)
if right(formPath,1)<>"/" then formPath=formPath&"/"
end if

lm=upload.form("filepath")

set rs=server.createobject("ADODB.recordset")
rs.Open "SELECT * from MyHuoZai",conn,1,3

if not rs.eof then
s=""
do while not rs.eof
if trim(rs("dt"))<>"" then
aa=""
aa=split(rs("dt"),"/")
s=s&","&aa(0)
end if
rs.movenext
loop
end if
s=split(s,",")
n=ubound(s)

for j=1 to n
if lm=s(j) then
ss=1
exit for
else
ss=0
end if
next

if ss=1 then
Response.Write("<script language=""JavaScript"">alert(""文件夹已经存在,路径错误!"");history.go(-1);</script>")
Response.End
end if

set myfileobject = server.createobject("Scripting.FileSystemObject")
myfileobject.CreateFolder(server.mappath("..")&"\hz\"&lm&"")

'Response.End
iCount=0
for each formName in upload.file ''列出所有上传了的文件
set file=upload.file(formName) ''生成一个文件对象
if file.FileSize>0 then ''如果 FileSize > 0 说明有文件数据
filename=file.filename
'makefilename(now)
filename=left(file.filename,1)&makefilename(right(file.filename,5))

file.SaveAs Server.mappath(formPath&filename)''保存文件

response.write file.FilePath&file.FileName&" ("&file.FileSize&") => "&formPath&File.FileName&" 成功!<br>"
iCount=iCount+1
end if
set file=nothing
next

set rs=server.createobject("ADODB.recordset")
rs.Open "SELECT * from MyHuoZai Where [id]='"&upload.form("id")&"'",conn,1,3
if not rs.eof then
SQL1="update MyHuoZai set dt='"&lm&"/"&filename&"' where id='"&upload.form("id")&"' and thechange=0"

conn.Execute SQL1
else
Response.Write("<script language=""JavaScript"">alert(""此火灾不存在!"");history.go(-1);</script>")
Response.End
end if

response.end

%>
</body>
</html>
gaopq_11 2002-12-28
  • 打赏
  • 举报
回复
剽窃来的无组件
文件夹名称:
upload:[upload0.asp,upp.asp,uploadx.asp,imag:存放上传的图片/文件的文件夹]

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
upload0.asp
<form method="POST" action="upp.asp" enctype="multipart/form-data" >
<p> photo:
<input type="file" name="fruit" size="20">
<!--要同时上传多个文件就把上面的代码:<input type="file" name="fruit" size="20">多复制几次呵呵-->
<input type="submit" value="upload" name="subbutt">
<input type="reset" value="reset" name="rebutt">
</form> <%=request("file")%>


</html>
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ upp.asp
<%@ LANGUAGE = VBScript %>
<%Server.ScriptTimeOut=100000 %>//设置上传文件的最大运行时间
<!-- #include file="uploadx.asp" -->
<%
dim filename
path = Server.MapPath("./imag/")
filename = SaveFile("fruit",path,6000,0)//设置上传文件的最大上限
If filename<>"" Then
If filename <> "*TooBig*" Then
Response.redirect "upload0.asp?message="& filename &" "
Else
Response.redirect "upload0.asp?message=文件超出限制太大(<=6000K)"
End IF
End IF
%>

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ uploadx.asp

<%
Dim FormData, FormSize, Divider, bCrLf
FormSize = Request.TotalBytes
FormData = Request.BinaryRead(FormSize)
bCrLf = ChrB(13) & ChrB(10)
Divider = LeftB(FormData, InStrB(FormData, bCrLf) - 1)
Function SaveFile(FormFileField, Path, MaxSize, SavType)
Dim StreamObj,StreamObj1
Set StreamObj = Server.CreateObject("ADODB.Stream")
Set StreamObj1 = Server.CreateObject("ADODB.Stream")
StreamObj.Mode = 3
StreamObj1.Mode = 3
StreamObj.Type = 1
StreamObj1.Type = 1
SaveFile = ""
StartPos = LenB(Divider) + 2
FormFileField = Chr(34) & FormFileField & Chr(34)
If Right(Path,1) <> "\" Then
Path = Path & "\"
End If
Do While StartPos > 0
strlen = InStrB(StartPos, FormData, bCrLf) - StartPos
SearchStr = MidB(FormData, StartPos, strlen)
If InStr(bin2str(SearchStr), FormFileField) > 0 Then
FileName = bin2str(GetFileName(SearchStr,path,SavType))
If FileName <> "" Then
FileStart = InStrB(StartPos, FormData, bCrLf & bCrLf) + 4
FileLen = InStrB(StartPos, FormData, Divider) - 2 - FileStart
If FileLen <= MaxSize*1024 Then
FileContent = MidB(FormData, FileStart, FileLen)
StreamObj.Open
StreamObj1.Open
StreamObj.Write FormData
StreamObj.Position=FileStart-1
StreamObj.CopyTo StreamObj1,FileLen
If SavType =0 Then
SavType = 1
End If
StreamObj1.SaveToFile Path & FileName, SavType
StreamObj.Close
StreamObj1.Close
If SaveFile <> "" Then
SaveFile = SaveFile & "," & FileName
Else
SaveFile = FileName
End If
Else
If SaveFile <> "" Then
SaveFile = SaveFile & ",*TooBig*"
Else
SaveFile = "*TooBig*"
End If
End If
End If
End If
If InStrB(StartPos, FormData, Divider) < 1 Then
Exit Do
End If
StartPos = InStrB(StartPos, FormData, Divider) + LenB(Divider) + 2
Loop
End Function

Function GetFormVal(FormName)
GetFormVal = ""
StartPos = LenB(Divider) + 2
FormName = Chr(34) & FormName & Chr(34)
Do While StartPos > 0
strlen = InStrB(StartPos, FormData, bCrLf) - StartPos
SearchStr = MidB(FormData, StartPos, strlen)
If InStr(bin2str(SearchStr), FormName) > 0 Then
ValStart = InStrB(StartPos, FormData, bCrLf & bCrLf) + 4
ValLen = InStrB(StartPos, FormData, Divider) - 2 - ValStart
ValContent = MidB(FormData, ValStart, ValLen)
If GetFormVal <> "" Then
GetFormVal = GetFormVal & "," & bin2str(ValContent)
Else
GetFormVal = bin2str(ValContent)
End If
End If
If InStrB(StartPos, FormData, Divider) < 1 Then
Exit Do
End If
StartPos = InStrB(StartPos, FormData, Divider) + LenB(Divider) + 2
Loop
End Function

Function bin2str(binstr)
Dim varlen, clow, ccc, skipflag
skipflag = 0
ccc = ""
varlen = LenB(binstr)
For i = 1 To varlen
If skipflag = 0 Then
clow = MidB(binstr, i, 1)
If AscB(clow) > 127 Then
ccc = ccc & Chr(AscW(MidB(binstr, i + 1, 1) & clow))
skipflag = 1
Else
ccc = ccc & Chr(AscB(clow))
End If
Else
skipflag = 0
End If
Next
bin2str = ccc
End Function

Function str2bin(str)
For i = 1 To Len(str)
str2bin = str2bin & ChrB(Asc(Mid(str, i, 1)))
Next
End Function

Function GetFileName(str,path,savtype)
Set fs = Server.CreateObject("Scripting.FileSystemObject")
str = RightB(str,LenB(str)-InstrB(str,str2bin("filename="))-9)
GetFileName = ""
FileName = ""
For i = LenB(str) To 1 Step -1
If MidB(str, i, 1) = ChrB(Asc("\")) Then
FileName = MidB(str, i + 1, LenB(str) - i - 1)
Exit For
End If
Next
If savtype = 0 and fs.FileExists(path & bin2str(FileName)) = True Then
hFileName = FileName
rFileName = ""
For i = LenB(FileName) To 1 Step -1
If MidB(FileName, i, 1) = ChrB(Asc(".")) Then
hFileName = LeftB(FileName, i-1)
rFileName = RightB(FileName, LenB(FileName)-i+1)
Exit For
End If
Next
For i = 0 to 9999
'hFileName = hFileName & str2bin(i)
If fs.FileExists(path & bin2str(hFileName) & i & bin2str(rFileName)) = False Then
FileName = hFileName & str2bin(i) & rFileName
Exit For
End If
Next
End If
Set fs = Nothing
GetFileName = FileName
End Function
%>
husz2001 2002-12-28
  • 打赏
  • 举报
回复
To theghost(鬼魅) :
试了一下,upload_5xsoft.inc, 第 179 行 提示有误.

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧