asp如何在上传图片指定路径文件夹的同时,传入数据库路径?
发布网友
发布时间:2024-01-17 14:19
我来回答
共2个回答
热心网友
时间:2024-11-02 04:15
上传分为几个页面来的,所以你最好看完我写给你的东东,,你要仔细看...
这样你最好是加我百度HI,我用HI传给你,就两个文件.在这里说一大通,你可能还是不会明白...
这是上传第一个页面
<tr align="center" valign="middle">
<td height="56" align="left" id="upid"> 上传课程: </td>
<td height="56" align="left" id="upid"><input type="text" name="zuoyeup">
<a href="#" onClick="window.open('upfile.htm','go','scrollbars=no,resizable=no,width=480,height=25,left=50,top=0,screenX=50,screenY=50')">点击上传 </a><span class="STYLE2">(允许格式为avi|wmv|mov|rm|rmvb|wma</span><span class="STYLE3">)</span></td>
</tr>
你看到那个NAME没?你在写入数据时,就把它写进去就可以了,你点那个点击上传,就会另外弹出一个小窗口页面,那个页面名就叫upfile.htm,这一段你可以加到你想要加的地方
第二个页面:弹出小窗口进行上传的页面,也就是upfile.htm页面
<form name="form1" method="post" action="upfile_flash.asp?id=id" enctype="multipart/form-data" >
<input type="hidden" name="act" value="upload">
<table width="324" border="0" cellspacing="0" cellpadding="5" align="center" >
<tr align="center" valign="middle">
<td width="314" height="60" align="left" id="upid"> <input type="file" name="file1" style="width:200px;" class="tx1" value="">
<input type="submit" name="Submit" value="提交" class="bt">
<input type="reset" name="Submit2" value="重执" class="bt">
</td>
</tr>
</table>
</form>
这个页面是让你选择上传文件来的,然后把你选择的文件一些值传到upfile_flash.asp?id=id这个页面进行处理
最后第三个页面upfile_flash.asp,也就是处理页面了
<!-- #include file="conn.asp" -->
<%Server.ScriptTimeOut=5000
'ConnectionDatabase()
%>
<html>
<head>
<title>文件上传</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<meta http-equiv="Content-Language" Content="zh-CN">
</head>
<body bgcolor="#FFFFFF">
<!-- #include file="upload_5xsoft.inc" -->
<%
dim upload,file,formName,iCount,FolderNameNew
dim GuFolderPath,fso,GuFolder0,GuFileSize0,GuFileExt0,GuAutoName
set upload=new upload_5xsoft '建立上传对象
GuFolder0="uploadimg" '设定默认上传的目录,必须以“/”结束,可以为空
GuFileSize0=204800 '设定默认允许上传的最大文件,单位:K,1024K=1M
GuFileExt0="jpg|gif|avi|wmv|mov|rm|rmvb|wma|doc|xls|txt" '设定默认允许上传的文件类型
GuAutoName="1" '设定上传成功后的文件名是否自动重新命名或是使用原来的名称,1为是,0为否
'Response.write upload.Version&"<br><br>" '显示上传类的版本
if upload.form("GuFolderPath")<>"" then
GuFolderPath=upload.form("GuFolderPath")
call FolderNameCheck(GuFolderPath)
GuFolderPath=upload.form("GuFolderPath")
if right(GuFolderPath,1)<>"/" then GuFolderPath=GuFolderPath&"/"
elseif upload.form("GuFolderPath")="" and GuFolder0<>"" then
GuFolderPath=GuFolder0
call FolderNameCheck(GuFolderPath)
GuFolderPath=GuFolder0
if right(GuFolderPath,1)<>"/" then GuFolderPath=GuFolderPath&"/"
else
GuFolderPath=""
end if
iCount=0
for each formName in upload.objForm '列出所有form数据
'Response.write formName&"="&upload.form(formName)&"<br>"
next
Response.write "<br><br>"
for each formName in upload.objFile '列出所有上传了的文件
set file=upload.file(formName)
if file.FileSize>0 then
dim FileExtF,FileExtY,FileExtOK,ii,jj
FileExtF=split(File.FileName,".")
for jj=0 to ubound(FileExtF)
next
FileExtY=0
FileExtOK=split(GuFileExt0,"|")
for ii=0 to ubound(FileExtOK)
if FileExtOK(ii)=FileExtF(jj-1) then
FileExtY=1
exit for
end if
next
if FileExtY=0 then
response.Write "上传失败,不允许上传的文件类型"
elseif file.FileSize>GuFileSize0*1024 then
response.Write "上传失败,单个文件大小超过*,最大"&GuFileSize0&"*1024 字节,1K=1024字节"
else
dim FileNameOK
if GuAutoName="1" then
FileNameOK=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&iCount&"."&FileExtF(jj-1)
else
FileNameOK=file.FileName
end if
file.SaveAs Server.mappath(GuFolderPath&FileNameOK) '保存文件
dim pictext
'标题: upload.form("pictext")
'call savedata(file.FileName,file.FileSize)
Response.write "<script>alert('上传成功');opener.document.all.form1.zuoyeup.value ='"&FileNameOK&"';window.close();</script>"
'iCount=iCount+1
end if
else
response.Write "上传失败,请选择要上传的文件"
end if
set file=nothing
next
set upload=nothing
'response.Write "上传成功!"
Sub FolderNameCheck(FolderNameNew)
dim Letters,i,c
Letters="+=:;,[]<>\|*?"
for i=1 to len(FolderNameNew)
c=mid(FolderNameNew,i,1)
if inStr(Letters,c)<>0 then
Htmend "上传失败,文件夹名称含有特殊字符"
end if
next
GuFolderPath=server.MapPath(GuFolderPath)
Set fso=Server.CreateObject("Scripting.FileSystemObject")
if fso.FolderExists(GuFolderPath)=false then
fso.CreateFolder(GuFolderPath)
end if
Set fso=nothing
End sub
%>
</body>
</html>
这当中有一个很重要的地方,你一定要注意,就是在这一段中Response.write "<script>alert('上传成功');opener.document.all.form1.zuoyeup.value ='"&FileNameOK&"';window.close();</script>"
你看到有个zuoyeup没有?传值的名字来的,这个名字一定要与要写入数据的名字一样,不然,就无法将获取得到的图片地址传到第一个页面的框框中,反正你再看看上面第一个页面哪里有这个名字的,你要改名字的,这两个地方都要一样的名字就是了.
第四个,upload_5xsoft.inc页面内容
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
dim Data_5xsoft
Class upload_5xsoft
dim objForm,objFile,Version
Public function Form(strForm)
strForm=lcase(strForm)
if not objForm.exists(strForm) then
Form=""
else
Form=objForm(strForm)
end if
end function
Public function File(strFile)
strFile=lcase(strFile)
if not objFile.exists(strFile) then
set File=new FileInfo
else
set File=objFile(strFile)
end if
end function
Private Sub Class_Initialize
dim RequestData,sStart,vbCrlf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,theFile
dim iFileSize,sFilePath,sFileType,sFormValue,sFileName
dim iFindStart,iFindEnd
dim iFormStart,iFormEnd,sFormName
Version="化境HTTP上传程序 Version 2.0"
set objForm=Server.CreateObject("Scripting.Dictionary")
set objFile=Server.CreateObject("Scripting.Dictionary")
if Request.TotalBytes<1 then Exit Sub
set tStream = Server.CreateObject("adodb.stream")
set Data_5xsoft = Server.CreateObject("adodb.stream")
Data_5xsoft.Type = 1
Data_5xsoft.Mode =3
Data_5xsoft.Open
Data_5xsoft.Write Request.BinaryRead(Request.TotalBytes)
Data_5xsoft.Position=0
RequestData =Data_5xsoft.Read
iFormStart = 1
iFormEnd = LenB(RequestData)
vbCrlf = chrB(13) & chrB(10)
sStart = MidB(RequestData,1, InStrB(iFormStart,RequestData,vbCrlf)-1)
iStart = LenB (sStart)
iFormStart=iFormStart+iStart+1
while (iFormStart + 10) < iFormEnd
iInfoEnd = InStrB(iFormStart,RequestData,vbCrlf & vbCrlf)+3
tStream.Type = 1
tStream.Mode =3
tStream.Open
Data_5xsoft.Position = iFormStart
Data_5xsoft.CopyTo tStream,iInfoEnd-iFormStart
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sInfo = tStream.ReadText
tStream.Close
'取得表单项目名称
iFormStart = InStrB(iInfoEnd,RequestData,sStart)
iFindStart = InStr(22,sInfo,"name=""",1)+6
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFormName = lcase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
'如果是文件
if InStr (45,sInfo,"filename=""",1) > 0 then
set theFile=new FileInfo
'取得文件名
iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
theFile.FileName=getFileName(sFileName)
theFile.FilePath=getFilePath(sFileName)
'取得文件类型
iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14
iFindEnd = InStr(iFindStart,sInfo,vbCr)
theFile.FileType =Mid (sinfo,iFindStart,iFindEnd-iFindStart)
theFile.FileStart =iInfoEnd
theFile.FileSize = iFormStart -iInfoEnd -3
theFile.FormName=sFormName
if not objFile.Exists(sFormName) then
objFile.add sFormName,theFile
end if
else
'如果是表单项目
tStream.Type =1
tStream.Mode =3
tStream.Open
Data_5xsoft.Position = iInfoEnd
Data_5xsoft.CopyTo tStream,iFormStart-iInfoEnd-3
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sFormValue = tStream.ReadText
tStream.Close
if objForm.Exists(sFormName) then
objForm(sFormName)=objForm(sFormName)&", "&sFormValue
else
objForm.Add sFormName,sFormValue
end if
end if
iFormStart=iFormStart+iStart+1
wend
RequestData=""
set tStream =nothing
End Sub
Private Sub Class_Terminate
if Request.TotalBytes>0 then
objForm.RemoveAll
objFile.RemoveAll
set objForm=nothing
set objFile=nothing
Data_5xsoft.Close
set Data_5xsoft =nothing
end if
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
End Class
Class FileInfo
dim FormName,FileName,FilePath,FileSize,FileType,FileStart
Private Sub Class_Initialize
FileName = ""
FilePath = ""
FileSize = 0
FileStart= 0
FormName = ""
FileType = ""
End Sub
Public function SaveAs(FullPath)
dim dr,ErrorChar,i
SaveAs=true
if trim(fullpath)="" or FileStart=0 or FileName="" or right(fullpath,1)="/" then exit function
set dr=CreateObject("Adodb.Stream")
dr.Mode=3
dr.Type=1
dr.Open
Data_5xsoft.position=FileStart
Data_5xsoft.copyto dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=false
end function
End Class
</SCRIPT>
热心网友
时间:2024-11-02 04:16
再你输入上传成功提示信息前将信息写入数据就是了
或者 上传图片是一个独立的部分,上传完后将相关的信息,如图片的名称,地址
传递到Form中 然后再提交Form内的数据 这样再写入数据库
一般是第二种方法
毕竟单一张图片没什么意思
或者 提交数据的时候提交图片 这时候上传图片也可以
不过可能等待时间比较的长