当前位置:首页 » 操作系统 » asp图片数据库

asp图片数据库

发布时间: 2023-08-27 02:25:23

⑴ 在ASP中,如何把图片和文字同时保存到数据库中,并在需要时同时输出。

  • 假设有这样一个表单。

  • <form id="myform" enctype="multipart/form-data" action="upload.asp" method="post">

  • <input type="file" name="file1"><!-用于提交文件,既图片文件-->

  • <input type="text" name="mytext"><!-用于提交文字信息-->

  • <input type="submit" name="tj" value="提交"><!--表单提交按钮-->

  • </form>

  • 表单能提交图片,也能提交文字。内容提交到upload.asp去处理。下面是upload.asp里面的部分内容。

  • <%Response.Charset="utf-8"%>

  • <%

  • '查询字符串thisstr2在字符串thisstr1里面第N次出现的位置,如果没有出现,返回空。

  • '这个函数也很有用。比如获得文件名的时候,文件名 name="file1" filename="abc.jpg",先二进制转文本,然后找到双引号第三次出现的位置和第四次出现的位置,两个位置中间的内容就是文件名,还可以得到文件的扩展名"jpg"

  • function strN(thisN,thisstr1,thisstr2)

  • thistemp=1

  • for thiss=1 to len(thisstr1)

  • thisdatastart=instr(thistemp,thisstr1,thisstr2)

  • if thisdatastart=0 or thisdatastart=null then

  • exit for

  • end if

  • if thisdatastart<>0 or thisdatastart<>null then

  • thistemp=thisdatastart+len(thisstr2)

  • thiscishu=thiscishu+1

  • if thiscishu=thisN then

  • strN=thisdatastart

  • exit for

  • end if

  • end if

  • next

  • if thiscishu<thisN then

  • strN=""

  • end if

  • end function

  • '查询二进制数据流thisstr2在thisstr1里面出现的次数。这个函数在已知表单提交信息条数的情况下就用不到。但如果是表单提交的信息条数未知,比如批量上传图片的时候,不知道有多少个type="file"的input,就需要用这个函数先判断一下。既判断分割符在提交数据里面出现的次数。出现了n次则有n-1条数据提交。

  • function mynumberb(thisstr1,thisstr2)

  • thistemp=1

  • for thisn=1 to len(thisstr1)

  • thisdatastart=instrb(thistemp,thisstr1,thisstr2)

  • if thisdatastart=0 or thisdatastart=null then

  • exit for

  • end if

  • if thisdatastart<>0 or thisdatastart<>null then

  • thistemp=thisdatastart+len(thisstr2)

  • thiscishu=thiscishu+1

  • end if

  • next

  • mynumberb=thiscishu

  • end function

  • '查询二进制数据流thisstr2在thisstr1里面第thisN次出现的位置,如果没有出现,返回空。

  • '这个函数很有用,比如表单传过来的数据都是用回车换行符号隔开的。只需要查询回车换行符号第4次出现的位置和第五次出现的位置,就能找到文件二进制数据开始和结束的位置。如果表单发送过来的是文本信息,只需要找到回车换行符号第三次出现的位置和第四次出现的位置,就能找到文本的二进制数据。然后二进制转文本,就提取出文本内容了。

  • function strNb(thisN,thisstr1,thisstr2)

  • thistemp=1

  • for thiss=1 to len(thisstr1)

  • thisdatastart=instrb(thistemp,thisstr1,thisstr2)

  • if thisdatastart=0 or thisdatastart=null then

  • exit for

  • end if

  • if thisdatastart<>0 or thisdatastart<>null then

  • thistemp=thisdatastart+len(thisstr2)

  • thiscishu=thiscishu+1

  • if thiscishu=thisN then

  • strNb=thisdatastart

  • exit for

  • end if

  • end if

  • next

  • if thiscishu<thisN then

  • strNb=""

  • end if

  • end function

  • '二进制转文本

  • Function stb(vin)

  • const adTypeText=2

  • dim BytesStream,StringReturn

  • Set BytesStream=Server.CreateObject("ADODB.Stream")

  • with BytesStream

  • BytesStream.Type=adTypeText

  • BytesStream.Open

  • BytesStream.WriteText vin

  • BytesStream.Position=0

  • BytesStream.Charset="utf-8"

  • BytesStream.Position=2

  • StringReturn=BytesStream.ReadText

  • BytesStream.Close

  • end with

  • set BytesStream=Nothing

  • stb=StringReturn

  • end function

  • '以上几个函数介绍完毕。接下来就是实际处理表单提交的信息了。

  • response.buffer=true

  • formsize=request.totalbytes

  • formdata=request.binaryread(formsize)

  • hcf=chrB(13)&chrB(10)'回车换行符号

  • fgf=leftB(formdata,clng(instrb(formdata,hcf))-1)'分隔符

  • cd=lenb(fgf)'分割符的长度

  • '截取第一条数据,既文件数据。

  • mydatastart=strnb(1,formdata,fgf)+cd

  • mydataend=strnb(2,formdata,fgf)-1

  • mydatasize=mydataend-mydatastart+1

  • formdata1=midb(formdata,mydatastart,mydatasize)'第一条提交的数据信息,既第一个type=file的图片文件

  • '得到文件的名字

  • mytempdata=stb(formdata1)

  • mydatastart=strn(3,mytempdata,"""")+1'双引号第三次出现的位置加1就是文件名出现的开始位置

  • mydataend=strn(4,mytempdata,"""")-1'双引号第四次出现的位置就是文件名结束的位置

  • mydatasize=mydataend-mydatastart+1

  • wjfilename=mid(mytempdata,mydatastart,mydatasize)'得到文件名字,就是提交的那个图片的名字,比如"myimg.jpg"

  • '截取图片文件的二进制数据

  • mydatastart=strnb(4,formdata1,hcf)+2'回车符号第四次出现的位置加2就是图片文件的二进制数据开始的位置

  • mydataend=strnb(5,formdata1,hcf)-1'回车符号第五次出现的位置减1就是图片二进制数据结束的位置

  • mydatasize=mydataend-mydatastart+1'图片文件二进制数据的长度

  • wjdata=midb(formdata1,mydatastart,mydatasize)'得到图片文件的二进制数据

  • '截取第二条数据,既截取提交的文本二进制数据

  • mydatastart=strnb(2,formdata,fgf)+cd

  • mydataend=strnb(3,formdata,fgf)-1

  • mydatasize=mydataend-mydatastart+1

  • formdata2=midb(formdata,mydatastart,mydatasize)'第二条提交的数据信息,既提交的文字信息。

  • '提取文本

  • mydatastart=strnb(3,formdata2,hcf)+2

  • mydataend=strnb(4,formdata2,hcf)-1

  • mydatasize=mydataend-mydatastart+1

  • wbdata=midb(formdata2,mydatastart,mydatasize)

  • wb=stb(wbdata)

  • '到此,表单信息全部接收完毕。

  • 'wjfilename:文件名。

  • 'wjdata:文件二进制数据。

  • 'wb:文字信息。

  • '下面要做的就是把文本信息存入数据库。把文件的二进制数据转换成图片存入文件夹,也可以直接二进制数据存放到数据库里面。

  • '至于怎么存放路径等一系列问题,这些都是简单问题。最难啃的骨头已经啃完了。

  • '文件信息存入文件夹提供一种思路,这种思路比较简单。

  • 'access里面的temp字段是存储二进制数据的,表名也叫temp

  • 'call conn_open(conn,"xxx.mdb")打开access

  • 'sql="select * from temp"

  • 'call rs_open3(rs,sql)打开表

  • ' rs.addnew

  • ' rs("temp").appendchunk wjdata

  • ' rs.update

  • ' Set MyStream=Server.CreateObject("Adodb.Stream")

  • ' MyStream.Type=1

  • ' MyStream.Open

  • ' MyStream.Write rs("temp").getChunk(8000000) 把数据库里面的图片读出来

  • '得到图片上传的日期时间连接1到1000之间的随机数做图片的名字,把时间里面的左斜杠,冒号以及空格都替换掉。

  • ' picName=replace(now(),"/","")

  • ' picName=replace(picName,":","")

  • ' Randomize

  • ' picName=replace(picName," ","")&Int((1000 * Rnd) + 1)

  • ' MyStream.SaveToFile server.mappath("img/"&picName&".jpg")

  • '把图片存入目录,注意,这里如果事先前端做了判'断用户提交的图片就必定是"jpg"格式,所以可以直接用。如果前端没做判断,就用刚'才得到的split(wjfilename,".")(1)作为扩展名。这玩意儿能看懂吧,wjfilename里面保存的是图片名字"abcd.jpg",用"."分割,

  • '后面的那个就是"jpg"图片扩展名

  • ' MyStream.close

  • ' set MyStream=nothing

  • 'call rs_close(rs)关闭表

  • 'call conn_close(conn)关闭access

  • %>

    以上提供的思路既然可以图片文字一起提交,也可以在不知道表单提交数据条数的情况下批量混合提交图片和文字信息。原理是死的,人的思路是活的。活学活用最好。

⑵ asp如何实现表单上传图片后存入access数据库

asp中表单上传图片后会解析成二进制byte数组保存到access数据库。
1、上传图片:

OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
pictureBox1.Image = new Bitmap(open.FileName);
// image file path
textBox1.Text = open.FileName;
}
2、保存图片信息到acess数据库。
C#实现,保存核心代码:

var pic = File.ReadAllBytes(yourFileName);
using(OleDbConnection con = new OleDbConnection(constr))
using(OleDbCommand cmd = new OleDbCommand("Insert Into DML_Books_List(ID, [Image]) values (@id, @image)", con))
{
con.Open();
cmd.Parameters.AddWithValue("@id", TextBox1.Text);
cmd.Parameters.AddWithValue("@image", pic);
cmd.ExecuteNonQuery();
}

⑶ ASP如何实现批量上传图片,并在数据库中有记录

<%

dim conn

dim rs

dim rs2


set conn=server.createobject("adodb.connection")

conn.connectionstring="Provider = Microsoft.Jet.OLEDB.4.0;Data Source="&server.mapPath("db.mdb")

conn.open


formsize=request.totalbytes


if formsize<>0 then '这里只判断了是否等于0,等于0就说明没有传过来数据。以后可以要多做点判断,比如限制图片大小的时候要判断一下。

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)'这里也要检测一下,就是检测这个数据里面是不是有病毒词汇之类的,这个可以在网上查一下病毒里面可能出现的词语,如果有这些词语,直接在这里就把这个mydata销毁,然后response.redirect "xxx.asp",有病毒赶快跳转,不要处理带病毒的数据。


set rs=server.createobject("ADODB.recordset")

sql="select * from imgurl"

rs.open sql,conn,1,3

rs.addnew

rs("img").AppendChunk myData'把这个图片的二进制数据新增到img字段里面。

tpm=replace(now(),"/","_") '用的日期做名字,日期里面有"/"," ",":"之类的东西,这些东西全部替换成"_"。

tpm=replace(tpm,":","_")

tpm=replace(tpm," ","_")

rs("图片名")=tpm

rs.update


Set MyStream=Server.CreateObject("Adodb.Stream")

MyStream.Type=1

MyStream.Open

MyStream.Write rs("img").getChunk(8000000)

MyStream.SaveToFile server.mappath("pic/"&tpm&".jpg")

MyStream.close

set MyStream=nothing


rs("img")="" '把access里面的二进制图片内容删除,只保留图片的名字。

rs.update


rs.close

set rs=nothing


response.write "<script>alert(""上传成功"");location.href=""upload.asp"";</script>"

end if

%>


<form action="upload.asp" method="post" enctype="multipart/form-data">

<input type="file" name="imgurl">

<input type="submit" name="ok" value="上传">

</form>



<!--------------下面是把图片显示出来------------------->



<div style="margin-top:2em;"><!--------------创建一个div把图片显示区域定位一下------------------->

<%

set rs2=server.createobject("ADODB.recordset")

sql2="select * from imgurl order by id desc"

rs2.open sql2,conn,1,1

for s=1 to rs2.recordcount

if not rs2.eof and not rs2.bof then

%>

<img src="pic/<%=rs2("图片名")%>.jpg">

<%

else

exit for

end if

rs2.movenext

next

rs2.close

set rs2=nothing

%>

</div>

<%

conn.Close

Set conn = Nothing

%>


<!--至于删除图片,这个就很简单了,图片的名字都已经进access里面了,直接读一下access里面的名字,读了一个名字,然后用fso在pic文件夹里面去删除对应的图片文件就可以了。--->

效果图:网页链接

热点内容
android亮度获取 发布:2025-02-01 12:09:10 浏览:624
小孩什么时候学编程比较好 发布:2025-02-01 12:03:10 浏览:960
c语言的认识 发布:2025-02-01 11:58:03 浏览:520
svn连接服务器地址 发布:2025-02-01 11:51:31 浏览:416
对源程序为什么要编译 发布:2025-02-01 11:47:46 浏览:218
sql表添加记录 发布:2025-02-01 11:22:08 浏览:864
word编辑加密 发布:2025-02-01 11:18:53 浏览:571
php变量文本 发布:2025-02-01 11:10:46 浏览:426
音悦台上传mv 发布:2025-02-01 11:05:02 浏览:516
微信如何设置访问限制 发布:2025-02-01 10:43:06 浏览:335