急 :怎样在同一个表单中既能提交text类型也能提交file类型的输入框

peiyanli 2009-08-15 11:11:10
一个表单中有一个<Input name="filename" type="file" size="15"> 用于输入姓名
和<Input name="name" type="text" size="15"> 用于上传照片
一个提交按钮 <Input name="submit" type="submit" value="添加">

在form中添加了一个 enctype="multipart/form-data" 属性后 text类型的内容无法通过post方法提交给其他页面
不添加又不能上传文件

该怎么办?????????????????
...全文
195 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xzx99 2009-08-18
  • 打赏
  • 举报
回复
收藏
peiyanli 2009-08-15
  • 打赏
  • 举报
回复
代码看不懂啊 这些代码放在什么地方?我是菜鸟。。。。。。。。。
number123456 2009-08-15
  • 打赏
  • 举报
回复
无组件上传

Dim oUpFileStream
Class UpFile_Class

Dim Form,File,Version,Err

Private Sub Class_Initialize
Version = "无组件上传类 Asp无组件文件上传到租用服务器空间的实例 Version V1.0"
Err = -1
End Sub

Private Sub Class_Terminate
'清除变量及对像
If Err < 0 Then
Form.RemoveAll
Set Form = Nothing
File.RemoveAll
Set File = Nothing
oUpFileStream.Close
Set oUpFileStream = Nothing
End If
End Sub

Public Sub GetData (RetSize)
'定义变量
Dim RequestBinDate,sSpace,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
End If

If int(RetSize) > 0 Then
If int(Request.TotalBytes) > int(RetSize) Then
Response.write "图片大小超过了限制 "&RetSize/1024&" K [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
Err = 2
End If
End If

Set Form = Server.CreateObject ("Scripting.Dictionary")
Form.CompareMode = 1
Set File = Server.CreateObject ("Scripting.Dictionary")
File.CompareMode = 1
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)
'取得每个项目之间的分隔符
sSpace = MidB (RequestBinDate,1, InStrB (1,RequestBinDate,bCrLf)-1)
iStart = LenB (sSpace)
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,sSpace)-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_Class
'取得文件属性
iFindStart = InStr (iFindEnd,sInfo,"filename=""",1)+10
iFindEnd = InStr (iFindStart,sInfo,"""",1)
sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
oFileInfo.FileName = Mid (sFileName,InStrRev (sFileName, "\")+1)
oFileInfo.FilePath = Left (sFileName,InStrRev (sFileName, "\")+1)
oFileInfo.FileExt = Mid (sFileName,InStrRev (sFileName, ".")+1)
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
If Form.Exists (sFormName) Then
Form (sFormName) = Form (sFormName) & ", " & SafeValue(sFormValue)
else
form.Add sFormName,SafeValue(sFormValue)
End If
End If
tStream.Close
iFormStart = iFormStart+iStart+2
'如果到文件尾了就退出
Loop Until (iFormStart+2) = iFormEnd
RequestBinDate = ""
Set tStream = Nothing
End Sub
End Class
Function SafeValue(str)
if str <> "" then
SafeValue = Replace(str,"'","''")
SafeValue = Replace(str,"""","“")
end if
End Function
'文件属性类
Class FileInfo_Class
Dim FormName,FileName,FilePath,FileSize,FileType,FileStart,FileExt
'保存文件方法
Public Function SaveToFile (Path)
'On Error Resume Next
Dim oFileStream
Set oFileStream = CreateObject ("ADODB.Stream")
oFileStream.Type = 1
oFileStream.Mode = 3
oFileStream.Open
oUpFileStream.Position = FileStart
oUpFileStream.CopyTo oFileStream,FileSize
oFileStream.SaveToFile Path,2
oFileStream.Close
Set oFileStream = Nothing
if Err.Number<>0 then
SaveToFile=err.number&"**"&Err.descripton
else
SaveToFile="ok"
end if
End Function

'取得文件数据
Public Function FileDate
oUpFileStream.Position = FileStart
FileDate = oUpFileStream.Read (FileSize)
End Function
End Class

dim upload,file,formName,SavePath,filename,fileExt
dim ranNum
'===========无组件上传(upload_0)====================
Function UpFile(SavePath,SaveFolder,SaveName)
set upload=new UpFile_Class '建立上传对象
upload.GetData (500*1024) '取得上传数据,此处即为500 K
Response.write upload.err
if upload.err > 0 then
select case upload.err
case 1
Response.Write "请先选择你要上传的文件 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
case 2
Response.Write "图片大小超过了限制 500 K [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
end select
exit Function
else
'在目录后加(/)
if right(SavePath,1)<>"/" then
SavePath=SavePath&"/"
end if
If Right(SaveFolder,1) <> "/" then
SaveFolder = SaveFolder & "/"
End If
If Left(SaveFolder,1) = "/" then
SaveFolder = Right(SaveFolder,Len(SaveFolder)-1)
End If
'创建文件夹
call CheckDir(SavePath&SaveFolder)
for each formName in upload.file '列出所有上传了的文件
set file=upload.file(formName) '生成一个文件对象
if file.filesize<1 then
response.write "请先选择你要上传的图片 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
response.end
end if

fileExt=lcase(file.FileExt)
if CheckFileExt(fileEXT)=false then
response.write "文件格式不正确 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
response.end
end if
If SaveName <> "" then
ToName = SaveName & "." & fileExt
Else
'randomize
ranNum=int(90000*rnd)+10000
ToName = year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum & "." & fileExt
End If
' If Instr(ToName,".") = 0 Then
' ToName = ToName & "." & fileExt
' End If
filename = SavePath & SaveFolder & ToName
if file.FileSize>0 then '如果 FileSize > 0 说明有文件数据
result=file.SaveToFile(Server.mappath(filename)) '保存文件
if result="ok" then
UpFile = SaveFolder & ToName
end if
end if
set file=nothing
next
set upload=nothing
end if
end Function

'检查是否存在目录,生成目录
Private Function CheckDir(FolderPath)
Dim fs0
Folder=Server.MapPath(FolderPath)'&"\"&folderpath
Set fso= CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(Folder) then
Else
Call MakeFolder(FolderPath)
End if
Set fso= nothing
End Function
'建立文件夹
Private Sub MakeFolder(FolderPath)
Dim fs0
FolderPath=Server.MapPath(FolderPath)'&"\"&folderpath
Set fso= CreateObject("Scripting.FileSystemObject")
fs0 = fso.CreateFolder(FolderPath)
Set fso= nothing
End Sub
'删除文件
Function DeleteFile(FolderPath)
Set fso = CreateObject("Scripting.FileSystemObject")
if fso.FileExists(server.MapPath(FolderPath)) then
fso.DeleteFile server.MapPath(FolderPath)
End If
Set fso = nothing
End Function

'判断文件类型是否合格
Private Function CheckFileExt (fileEXT)
dim Forumupload
Forumupload="gif,jpg,bmp,jpeg"
Forumupload=split(Forumupload,",")
for i=0 to ubound(Forumupload)
if lcase(fileEXT)=lcase(trim(Forumupload(i))) then
CheckFileExt=true
exit Function
else
CheckFileExt=false
end if
next
End Function


demo

Set up = new UpFile_Class
up.form("text") '获得非file类型
up.file("file") '获得FILE类型
sy_binbin 2009-08-15
  • 打赏
  • 举报
回复
你去下个无组件上传类
看看里面的DEMO就知道怎么做了!
poiuy343 2009-08-15
  • 打赏
  • 举报
回复
其实只要你知道2进制上传的原理就能截取出表单的内容了

以二进制multipart/form-data方式上传的东西 转化成字符串为
xxxxxx 输入框内容 xxxxxxx 图片类型 xxxxxxxx 图片大小xxxx 图片内容xxxxx
的形式。 所谓无组件上传 就是利用字符串函数, 把上传的2进制中的 图片内容 块截取下来 然后存为二进制, 既然可以截取图片内容,当然也可以截取输入框内容咯,然后再转化成字符串 就可以存起来了~
不知道你听懂没。。没听懂就可以取看看无组件上传原理!
peiyanli 2009-08-15
  • 打赏
  • 举报
回复
我数据库中保存的图像的格式是二进制数据
number123456 2009-08-15
  • 打赏
  • 举报
回复
第一段代码中存成upload.asp
建一个upfile.asp
<!--#include file="upload.asp"-->
<%
if action = "up" then
Set up = new UpFile_Class
up.GetData (SaveSize)
SaveSize = 2048000'限制上传字节大小
title = up.form("title") '获得非file类型
'上传文件
Size = up.file("file").FileSize'上传文件的大小
Ext = up.file("file").FileExt'上传文件的类型
up.file("file").SaveToFile(Server.MapPath("保存的图径"))
rs.open "select * from file",conn,1,3
rs.addnew
rs("title") = title
rs.update
rs.close
end if
%>
<form action="?action=up" method="post" enctype="multipart/form-data" name="form1" id="form1" >
<input type="text" name="title" />
<input type="file" name="file" />
</form>

28,406

社区成员

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

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