一个图片说明与图片一同上传的问题

nc2015 2004-04-02 09:44:38


在影集中每张图片必定会有一段与之相关的说明,但是我不知道怎么才能使说明与图片一同上传到数据库。


下面是我的例子:
=-=-=-=-=-=-=-=-=-=-=-=-=-=-
access
img ole对像
text 备注
=-=-=-=-=-=-=-=-=-==-=-=-=-=



upphoto.htm
----------------------------------
<form enctype="multipart/form-data" action="addpho.asp" method="post">
<input name="img" type="file" size="35">
<textarea name="imgtext" cols="53" rows="8" >相关介绍:</textarea>
<input name="ok" type="submit" value=" 确定 ">
</form>


addpho.asp
----------------------------------
<%
dim rs
dim formsize,formdata,bncrlf,divider,datastart,dataend,mydata
formsize=request.totalbytes
formdata=request.binaryread(formsize)
bncrlf=chrB(13) & chrB(10)
divider=leftB(formdata,clng(instrb(formdata,bncrlf))-1)
datastart=instrb(formdata,bncrlf & bncrlf)+4
dataend=instrb(datastart+1,formdata,divider)-datastart
mydata=midb(formdata,datastart,dataend)
%>
<!--#include file="com.asp"-->
<%
sql="select * from img"
set rs=conn.execute(sql)
'id=rs("id")
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "img",conn,3,2
rs.addnew
rs("img").appendchunk mydata
rs.update
rs.close
set rs=nothing
set conn=nothing
%>
<script language="JavaScript">
<!--
window.opener.location="photo.asp"
window.close();
-->
</script>
----------------------------------------完

在addpho.asp中我加入rs("text")=request.form("imgtext")就会报错。


我应该怎么做,谢谢。

...全文
43 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
pizixt 2004-04-03
  • 打赏
  • 举报
回复
接上(upload.asp):

'把二进制字符串转换成普通字符串函数
Function bin2str(binstr)
Dim varlen,clow,ccc,skipflag
'中文字符Skip标志
skipflag=0
ccc = ""
If Not IsNull(binstr) Then
varlen=LenB(binstr)
For i=1 To varlen
If skipflag=0 Then
clow = MidB(binstr,i,1)
'判断是否中文的字符
If AscB(clow) > 127 Then
'AscW会把二进制的中文双字节字符高位和低位反转,所以要先把中文的高低位反转
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
End If
bin2str = ccc
End Function


'把普通字符串转成二进制字符串函数
Function str2bin(varstr)
str2bin=""
For i=1 To Len(varstr)
varchar=mid(varstr,i,1)
varasc = Asc(varchar)
' asc对中文字符求出来的值可能为负数,
' 加上65536就可求出它的无符号数值
' -1在机器内是用补码表示的0xffff,
' 其无符号值为65535,65535=-1+65536
' 其他负数依次类推。
If varasc<0 Then
varasc = varasc + 65535
End If
'对中文的处理:把双字节低位和高位分开
If varasc>255 Then
varlow = Left(Hex(Asc(varchar)),2)
varhigh = right(Hex(Asc(varchar)),2)
str2bin = str2bin & chrB("&H" & varlow) & chrB("&H" & varhigh)
Else
str2bin = str2bin & chrB(AscB(varchar))
End If
Next
End Function

'取得文件名(去掉Path)
Function GetFileName(FullPath)
If FullPath <> "" Then
FullPath = StrReverse(FullPath)
FullPath = Left(FullPath, InStr(1, FullPath, "\") - 1)
GetFileName = StrReverse(FullPath)
Else
GetFileName = ""
End If
End Function
</SCRIPT>
<SCRIPT RUNAT=SERVER LANGUAGE=JSCRIPT>
function CreateUploadField(){ return new uf_Init() }
function uf_Init(){
this.Name = null
this.FileName = null
this.FilePath = null
this.ContentType = null
this.Value = null
this.Length = null
}
</SCRIPT>
pizixt 2004-04-02
  • 打赏
  • 举报
回复
<form name="picform" enctype="multipart/form-data" action="uploadpic.asp" method="post">
选择图片:<input name="img" type=file size="40"> 限*.jpg和*.gif格式<br>
图片说明:<input type="text" name="nr" style="width: 287; height: 20"><br>
<input type=submit value="上传">
</form>

uploadpic.asp:
<!--#INCLUDE FILE="upload.asp"-->
<%
FormSize=Request.TotalBytes
FormData=Request.BinaryRead(FormSize)
Set Fields = GetUpload(FormData)

imgsize= Fields("img").length
imgtype= Fields("img").ContentType
imgdata=Fields("img").Value
nr=Fields("nr").Value

if imgtype<>"image/pjpeg" and imgtype<>"image/gif" then
response.write "<script>alert('图片格式错误');history.back()</script>"
response.end
else
Set rs=Server.CreateObject("ADODB.Recordset")
rs.open "photo",Conn,1,3
rs.addnew
rs("img").appendchunk imgdata
rs("size")=imgsize
if nr="" then
rs("nr")="不说什么,看吧"
else
rs("content")=nr
end if
rs.update
response.write "<script>alert('发布成功');location.href='xxx.asp'</script>"
end if
%>

upload.asp:
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
Function GetUpload(FormData)
Dim DataStart,DivStr,DivLen,DataSize,FormFieldData
'分隔标志串(+CRLF)
DivStr = LeftB(FormData,InStrB(FormData,str2bin(VbCrLf)) + 1)
'分隔标志串长度
DivLen = LenB(DivStr)
PosOpenBoundary = InStrB(FormData,DivStr)
PosCloseBoundary = InStrB(PosOpenBoundary + 1,FormData,DivStr)
Set Fields = CreateObject("Scripting.Dictionary")

While PosOpenBoundary > 0 And PosCloseBoundary > 0
'name起始位置(name="xxxxx"),加6是因为[name="]长度为6
FieldNameStart = InStrB(PosOpenBoundary,FormData,str2bin("name=")) + 6
FieldNameSize = InStrB(FieldNameStart,FormData,ChrB(34)) - FieldNameStart '(")的ASC值=34
FormFieldName = bin2str(MidB(FormData,FieldNameStart,FieldNameSize))

'filename起始位置(filename="xxxxx")
FieldFileNameStart = InStrB(PosOpenBoundary,FormData,str2bin("filename=")) + 10
If FieldFileNameStart < PosCloseBoundary And FieldFileNameStart > PosopenBoundary Then
FieldFileNameSize = InStrB(FieldFileNameStart,FormData,ChrB(34)) - FieldFileNameStart '(")的ASC值=34
FormFileName = bin2str(MidB(FormData,FieldFileNameStart,FieldFileNameSize))
Else
FormFileName = ""
End If

'Content-Type起始位置(Content-Type: xxxxx)
FieldFileCTStart = InStrB(PosOpenBoundary,FormData,str2bin("Content-Type:")) + 14
If FieldFileCTStart < PosCloseBoundary And FieldFileCTStart > PosOpenBoundary Then
FieldFileCTSize = InStrB(FieldFileCTStart,FormData,str2bin(VbCrLf & VbCrLf)) - FieldFileCTStart
FormFileCT = bin2str(MidB(FormData,FieldFileCTStart,FieldFileCTSize))
Else
FormFileCT = ""
End If

'数据起始位置:2个CRLF开始
DataStart = InStrB(PosOpenBoundary,FormData,str2bin(VbCrLf & VbCrLf)) + 4
If FormFileName <> "" Then
'数据长度,减1是因为数据文件的存取字节数问题(可能是AppendChunk方法的问题):
'由于字节数为奇数的图象存到数据库时会去掉最后一个字符导致图象不能正确显示,
'字节数为偶数的数据文件就不会出现这个问题,因此必须保持字节数为偶数。
DataSize = InStrB(DataStart,FormData,DivStr) - DataStart - 1
FormFieldData = MidB(FormData,DataStart,DataSize)
Else
'数据长度,减2是因为分隔标志串前有一个CRLF
DataSize = InStrB(DataStart,FormData,DivStr) - DataStart - 2
FormFieldData = bin2str(MidB(FormData,DataStart,DataSize))
End If

'建立一个Dictionary集存储Form中各个Field的相关数据
Set Field = CreateUploadField()
Field.Name = FormFieldName
Field.FilePath = FormFileName
Field.FileName = GetFileName(FormFileName)
Field.ContentType = FormFileCT
Field.Length = LenB(FormFieldData)
Field.Value = FormFieldData

Fields.Add FormFieldName, Field

PosOpenBoundary = PosCloseBoundary
PosCloseBoundary = InStrB(PosOpenBoundary + 1,FormData,DivStr)
Wend
Set GetUpload = Fields
End Function
说好不能打脸 2004-04-02
  • 打赏
  • 举报
回复
不想看源码给你说说思路吧,主要是用url传送图片说明,并在使用上船组件前先request传过来的值,再上传完照片后用session纪录所有图片地址和所有图片说明,然后response重定向,回到表单页后就可以显示以上传图片和图片说明了
说好不能打脸 2004-04-02
  • 打赏
  • 举报
回复
pic_upload.asp

<%
id = request("id")
function selectime()
year1 = year(date())
month1 = month(date())
if month1 < 10 then
month1 = "0" & month1
end if
day1 = day(date())
if day1 < 10 then
day1 = "0" & day1
end if
time1 = replace(time(),":","")
goodtime = year1 & month1 & day1 & time1
selectime = goodtime
end function
%>



<%response.buffer=true%><%'上传图片
dim size3,file1,file2,c,c2,cs,ce,data3

str = Request("str")
if str = "" then
str = "没有说明"
end if
Set file1=Server.CreateObject ("ADODB.Stream")
file1.Type=1
file1.Mode=3
file1.Open
'Response.Write "Request.TotalBytes =" & Request.TotalBytes & "<br>"
'Response.Write "Request.BinaryRead (Request.TotalBytes) =" & Request.BinaryRead (Request.TotalBytes) & "<br>"
'Response.End

file1.Write(Request.BinaryRead (Request.TotalBytes))
file1.Position=0
data3=file1.Read

c=chrB(13) & chrB(10)
c2=leftB(data3,clng(instrb(data3,c))-1)
cs=instrb(data3,c & c)+3
ce=instrb(cs,data3,c2)-1
size3=ce-cs


if size3<10 then

response.write "文件过小<br>"
Response.Write "点击此处<a href=javascript:history.go(-1)>返回</a>"

elseif size3>300000 then

response.write "文件太大<br>"
Response.Write "点击此处<a href=javascript:history.go(-1)>返回</a>"

else

size3=selectime()

Set file2=CreateObject ("ADODB.Stream")

file2.Type=1

file2.Mode=3

file2.Open

file1.Position=cs

file1.CopyTo file2,ce-cs-2

session("image_URL") = session("image_URL") & "pic/temp" & size3 & ".jpg" & "#" & str & ","

'Response.Write session("image_URL")
'Response.End

file2.SaveToFile Server.MapPath("temp" & size3 & ".jpg"),2

'Response.Write session("image_URL")
'Response.End
if id = "" then
Response.Redirect "../add_track.asp"
else
Response.Redirect "../add_track.asp?id=" & id
end if
end if

%>

此也用于上传,并纪录session
说好不能打脸 2004-04-02
  • 打赏
  • 举报
回复
已测试:

add_track.asp:

<form action="adding_track.asp" method = post name=combat_add_form onsubmit="return uu()">
<table>
<tr>
<td>请输入股票名:</td><td><input type=text name=stock_name></td>
</tr>
<tr>
<td>请输入点评内容:</td><td><textarea name=coutent></textarea></td>
</tr>
<tr>
<td>这是您对该股票的</td><td>第<font color=#FF0000> 1 </font>次跟踪</td>
</tr>
</table>
<input type=submit value="确定"> <input type=reset value="重添">
</form>


<hr>
<form action="pic/pic_upload.asp" method=post enctype="multipart/form-data" name=img_form>
<table>
<tr>
<td>
<input type=file name="img_file"><br>
</td>
</tr>
<tr>
<td>
<input type=text id="img_text" name="img_text" onkeypress="return check()"> 图片说明<br>
<input type=button name="img_button" value="上传此图片" onclick=tt() checked> <input type=reset value="重新选择图片">
</td>
</tr>
</table>
</form><br><hr>

您已上传的文件列表:
<table width=100%>
<tr>
<%if session("image_URL") = "" then%>
<td>您还没上传任何图片</td>
<%else%>
<%
'----------------------------------------拆分session("image_URL")
str = split(session("image_URL"),",")
'-------------------------------------------以上拆分session("image_URL")
'------------------------------------------上传图片列表
for i = 0 to ubound(str)

tt = str(i)

text = split(tt,"#")
Response.Write "<tr>"
for j = 0 to ubound(text)
if j = 0 then
Response.Write "<td>"
Response.Write "<a href=" & text(j) & " target=_blank>" & text(j) & "</a>"
Response.Write "</td>"
end if
if j = 1 then
Response.Write "<td>"
Response.Write text(j)
Response.Write "</td>"
end if
next
Response.Write "</tr>"
next
'------------------------------------------以上是上传图片列表
%>
<%end if%>
</tr>
</table>
您已上传的文件列表:
<table width=100%>
<tr>
<%if session("image_URL") = "" then%>
<td>您还没上传任何图片</td>
<%else%>
<%
'----------------------------------------拆分session("image_URL")
str = split(session("image_URL"),",")
'-------------------------------------------以上拆分session("image_URL")
'------------------------------------------上传图片列表
for i = 0 to ubound(str)

tt = str(i)

text = split(tt,"#")
Response.Write "<tr>"
for j = 0 to ubound(text)
if j = 0 then
Response.Write "<td>"
Response.Write "<a href=" & text(j) & ">" & text(j) & "</a>"
Response.Write "</td>"
end if
if j = 1 then
Response.Write "<td>"
Response.Write text(j)
Response.Write "</td>"
end if
next
Response.Write "</tr>"
next
'------------------------------------------以上是上传图片列表
%>
<%end if%>
</tr>
</table>

zhengxianping 2004-04-02
  • 打赏
  • 举报
回复
<%
sql="select * from img"
set rs=conn.execute(sql)
'id=rs("id")
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "img",conn,3,2
rs.addnew
rs("img").appendchunk mydata
rs.update
rs.close
set rs=nothing
set conn=nothing
%>
rs.addnew'不应该这样用的应该都把当成二进制处理。
最苯的方法是在数据库里先添加文本。再
sql="select * from img where id='上面添加的纪录标志'"


Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sql,conn,3,2
if not rs.eof then 意思就是如果存在就不能用 rs.addnew 这个命令
rs.addnew
end if
rs("img").appendchunk mydata
rs.update
rs.close
set rs=nothing
set conn=nothing
%>
lubaixu 2004-04-02
  • 打赏
  • 举报
回复
呵呵。
因为你保存图片的时候,是取它的二进制流,然后把值附到数据库中相应的字段中。
但是文本不能这样。
你可以先用session保存你的图片简介信息。
然后才保存到数据库中。
look4sword 2004-04-02
  • 打赏
  • 举报
回复
是不能这样的。
我看到一个程序中存文件到数据库是这样写的:
......
' Save the file to the database
RS("filename") = File.FileName
RS("filesize") = File.FileSize
File.SaveToDatabase RS("filedata")
RS.Update
RS.Close
......

28,390

社区成员

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

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