读书人

高分上传图片后文件路径获取不到有关

发布时间: 2012-02-06 15:52:45 作者: rapoo

高分求救上传图片后文件路径获取不到,问题解决,马上给分,谢!
问题是这样的:
我在表单中有这样一个图片上传控件:
<form action= "save.asp?abc=add_yes&id= <%= request( "id ") %> " method= "post " name= "form " id= "form " onsubmit= "javascript:return checksignup1(); ">

<tr>
<td align= "right " class= "font-14 "> 图片上传: </td>
<td> <script language = "JavaScript ">
function loadForm()
{
UploadFile_b.txtfile_b.value = document.form.photo.value;
UploadFile_b.txtfile_b.value = document.form.hiddenphoto.value;
document.form.hiddenphoto.value = UploadFile_b.txtfile_b.value;
return true
}
</script>
<input type= "hidden " name= "hiddenphoto " value= " " onChange= "loadForm() ">
<textarea name= "photo " style= "display:none " onChange= "loadForm "> </textarea>
<iframe style= "top:2px " ID= "UploadFile_b " src= "upload_b.asp " frameborder=0 scrolling=no width= "350 " height= "25 "> </iframe>
</td>
</tr>

<input name= "Submit " type= "submit " class= "font-a14 " value= "提交修改 ">

</form>

upload_b.asp文件如下:(可以原封不动复制)
<!--#include file= "../../mycartconn/conn.asp "-->
<SCRIPT language=javascript>
function check_file()
{
var strFileName=upform.txtfile_b.value;
if (strFileName== " ")
{
alert( "请选择要上传的文件 ");
return false;
}
}
</SCRIPT>
<%
Const EnableUploadFile= "Yes " '是否开放文件上传
Const MaxFileSize=100 '上传文件大小限制
Const SaveUpFilesPath= "../tp/biz " '存放上传文件的目录
Const UpFileType= "gif|jpg|bmp|png " '允许的上传文件类型

huiyuan=Request.Cookies(my_url)( "huiyuan ")
sql6= "select id,vip from huiyuan where huiyuan= ' "&huiyuan& " ' "
set rs6=server.createobject( "adodb.recordset ")
rs6.open sql6,conn,3,1
if rs6( "vip ")=1 then biz=1
rs6.close
set rs6=nothing

if EnableUploadFile= "Yes " then%>
<body leftmargin= "0 " topmargin= "0 ">
<form action= "upfile_b.asp " method= "post " name= "upform " onSubmit= "return check_file() " enctype= "multipart/form-data ">
<input name= "txtfile_b " type= "FILE " size= "30 "> <input name= "Submit " type= "submit " class= "input-b " value= "上传 ">


</form>

<% 'end if
end if%>

upfile_b.asp文件如下:(可以原封不动的复制)
<!--#include file= "upload.asp "-->

<%
Const EnableUploadFile= "Yes " '是否开放文件上传
Const MaxFileSize=100 '上传文件大小限制
Const SaveUpFilesPath= "../../tp/biz " '存放上传文件的目录
Const UpFileType= "gif|jpg|bmp|png " '允许的上传文件类型
const upload_type=0 '上传方法:0=无惧无组件上传类,1=FSO上传 2=lyfupload,3=aspupload,4=chinaaspupload

dim upload,file,formName,SavePath,filename,fileExt
dim upNum
dim EnableUpload
dim Forumupload
dim ranNum
dim uploadfiletype
dim msg,founderr
msg= " "
founderr=false
EnableUpload=false
SavePath = SaveUpFilesPath '存放上传文件的目录
if right(SavePath,1) <> "/ " then SavePath=SavePath& "/ " '在目录后加(/)

if EnableUploadFile= "No " then
response.write "系统未开放文件上传功能 "
else
select case upload_type
case 0
call upload_0() '使用化境无组件上传类
case else
'response.write "本系统未开放插件功能 "
'response.end
end select
end if
sub upload_0() '使用化境无组件上传类
set upload=new upload_file '建立上传对象
for each formName in upload.file '列出所有上传了的文件
set file=upload.file(formName) '生成一个文件对象
if file.filesize <10 then
msg= "请先选择你要上传的文件! "
founderr=true
end if
if file.filesize> (MaxFileSize*1024) then
msg= "文件大小超过了限制,最大只能上传 " & CStr(MaxFileSize) & "K的文件! "
founderr=true
end if

fileExt=lcase(file.FileExt)
Forumupload=split(UpFileType, "| ")
for i=0 to ubound(Forumupload)
if fileEXT=trim(Forumupload(i)) then
EnableUpload=true
exit for
end if
next
if fileEXT= "asp " or fileEXT= "asa " or fileEXT= "aspx " then
EnableUpload=false
end if
if EnableUpload=false then
msg= "这种文件类型不允许上传!\n\n只允许上传这几种文件类型: " & UpFileType
founderr=true
end if

strJS= " <SCRIPT language=javascript> " & vbcrlf
if founderr <> true then
randomize
ranNum=int(900*rnd)+100
filename=SavePath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum& ". "&fileExt

file.SaveToFile Server.mappath(FileName) '保存文件

msg= "上传文件成功! "
%>
<body leftmargin= "0 " topmargin= "0 " style= "font-size:12px;color:0055b0 " bgcolor= "f5f5f5 ">
<FORM method= "post " action= " " name= "upform ">


<p style= "margin-top:5 "> 上传成功! <input name= "txtfile_b " type= "text " class= "input-16 " size= "10 " value= " <%=FileName%> "> > > <a href= " <%=FileName%> " target= "_blank "> <font color= "0055b0 "> <u> 浏览图片 </u> </font> </a>
</form>
<%
end if
response.write strJS
set file=nothing
next
set upload=nothing
end sub
%>


[解决办法]
实在太长了,如果只是上传张图片,要得到图片名的话:


<link href=sitecss.css rel=stylesheet>
<%
sub error2(message)
%>
<script> alert( ' <%=message%> ');history.back(); </script> <script> window.close(); </script>
<%
end sub
%>

<%
dim oUpFileStream

Class Upload_file

dim Form,File,Err

Private Sub Class_Initialize
Err=-1
end sub

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

Public Sub GetDate(RetSize)
'定义变量
dim RequestBinDate,sStart,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo
dim iFileSize,sFilePath,sFileType,sFormvalue,sFileName
dim iFindStart,iFindEnd
dim iFormStart,iFormEnd,sFormName
'代码开始
If Request.TotalBytes < 1 Then
Err=1
Exit Sub
End If
If RetSize > 0 Then
If Request.TotalBytes > RetSize then
Err=2
Exit Sub
End If
End If
set Form = Server.CreateObject( "Scripting.Dictionary ")
set File = Server.CreateObject( "Scripting.Dictionary ")
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 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, ". ")+1)
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

'取得文件内容
Public Function GetDate
oUpFileStream.Position =FileStart
GetDate=oUpFileStream.Read(FileSize)
End Function
End Class
%>

<%
if Request( "menu ")= "up " then
On Error Resume Next
Set upl = Server.CreateObject( "SoftArtisans.FileUp ")
If -2147221005 = Err Then
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '

set FileUP=new Upload_file

FileUP.GetDate(-1)
formPath= "../topnews/ "
set file=FileUP.file( "file ")
filename=formPath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)& ". "&file.FileExt

if file.filesize > 307200 then
error2( "文件大小不得超过 300 K\n当前的文件大小为 "&int(file.filesize/1024)& " K ")


end if

select case LCase(file.FileExt)
case "gif "
img= " "&filename& " "
case "jpg "
img= " "&filename& " "
case else
error2( "对不起,本服务器只支持GIF、JPG格式的文件\n不支持 "&file.FileExt& " 格式的文件 ")
end select
'if " "&file.filesize& " "=Request.Cookies( "TotalBytes ") then
'error2( "请不要上传相同的文件! ")
'end if
file.SaveToFile Server.mappath(filename)
'Response.Cookies( "TotalBytes ")=file.filesize
Session( "TopImg ")=filename
set FileUP=nothing
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
else
filename= " "&year(date)& " "&month(date)& " "&day(date)& " "&hour(time)& " "&minute(time)& " "&second(time)& " "
select case " "&upl.ContentType& " "
case "application/octet-stream "
error2( "未知文件格式 ")
case "image/gif "
types= "gif "
case "image/pjpeg "
types= "jpg "
end select
filename= " "&filename& ". "&types& " "
if upl.TotalBytes > 307200 then
error2( "文件大小不得超过 300 K\n当前的文件大小为 "&int(upl.TotalBytes/1024)& " K ")
end if
if types= "gif " or types= "jpg " then
img= "[img] "&filename& "[/img] "
else
error2( "对不起,本服务器只支持GIF、JPG、格式的文件\n不支持 "&upl.ContentType& " 格式的文件 ")
end if
'if " "&upl.TotalBytes& " "= " "&Request.Cookies( "TotalBytes ")& " " then
'error2( "请不要上传相同的文件! ")
'end if
upl.SaveAs Server.mappath( " "&filename& " ")
Session( "Img ")=filename '在save.asp中将session( "img ")存到库中既可
'Response.Cookies( "TotalBytes ")= " "&upl.TotalBytes& " "
set upl=nothing
End If
%>
<link rel= "stylesheet " href= "css " type= "text/css ">
<style type= "text/css ">
<!--
body {
background-color: #eeeeee;
}
.STYLE1 {font-size: 12px}
-->
</style> <body topmargin=0 class=a1>
<link href=css.css rel=stylesheet>
<span class= "STYLE1 "> 上传成功,图片链接: <a href= " <%=filename%> " target= "_blank "> <%=filename%> </a> </span>
<%
else
%>
<!--response.write " <link href=css.css rel=stylesheet> <SCRIPT> parent.form1.topshow.value+= '\n "&img& " ' </SCRIPT> "--> '此处如果去掉注释,在form里放一个名为topshow的文本框,上传后,图片名字及路径会显示出来,也可在save.asp中保存request( "topshow ")
<link rel= "stylesheet " href= "css " type= "text/css ">
<style type= "text/css ">
<!--
body {
background-color: #eeeeee;
}
-->
</style> <body topmargin=0 class=a1>
<link href=css.css rel=stylesheet>
<form enctype=multipart/form-data method=post action=upfile.asp?menu=up>


<table cellpadding=0 cellspacing=0 width=100%>
<tr>
<td> <input name=file type=file class= "TDtop1 " style=FONT-SIZE:9pt size= "30 ">
<input name=Submit type= "submit " class= "Up " style=FONT-SIZE:9pt value= " 上 传 "> </td> </tr> </table>
<%
end if
%>
[解决办法]
你没有给hiddenphoto赋值,怎么能得到它的值呢,至少我是没有看到,呵呵.
在你的upfile_b.asp中的局部代码修改如下:
if founderr <> true then
randomize
ranNum=int(900*rnd)+100
filename=SavePath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum& ". "&fileExt

file.SaveToFile Server.mappath(FileName) '保存文件

msg= "上传文件成功! "
response.write " <script> window.opener.document.form.hiddenphoto.value= ' "&FileName& " ' </script> "
%>
<body leftmargin= "0 " topmargin= "0 " style= "font-size:12px;color:0055b0 " bgcolor= "f5f5f5 ">
<FORM method= "post " action= " " name= "upform ">
<p style= "margin-top:5 "> 上传成功! <input name= "txtfile_b " type= "text " class= "input-16 " size= "10 " value= " <%=FileName%> "> > > <a href= " <%=FileName%> " target= "_blank "> <font color= "0055b0 "> <u> 浏览图片 </u> </font> </a>
</form>
<%
end if


加了一行,给父级隐藏域赋值.

读书人网 >ASP

热点推荐