系带断裂影响勃起吗:ASP里面如何上传图片?

来源:百度文库 编辑:高校问答 时间:2024/04/30 18:50:44
请教具体代码及过程.谢谢

1.如果服务器装了文件上传组件,可查阅一下该组件的相关属性和方法,不过现在很多服务器空间都不支持这类组件.暂不在这里讨论.

2.若服务器不支持文件上传组件,可以考虑自己写个无组件上传程序,网上有一个"化境无组件上传"比较经典,也比较好用.地址:http://www.5xsoft.com/Down.aspx?id=2.
你可以下载一个参考,里面的说明很详细.
不过可能有的服务器也不会支持,因为该组件可能会被视为木马之类而被清除.

3.利用服务器的FSO组件可实现文本文件之类的在线读写,但要实现上传图片估计不能,我还没见过.

4.以上办法都不行时,我们可以考虑将图片上传到数据库.下面以Access为例,看一个简单的代码:

首先建一个数据库,假设为(img.mdb).在其中建一表(pic),表中有两个字段(id,img),id数据类型为"自动编号",img的类型为"OLE对象".
下面用三个文件用来实现图片功能,conn.asp是定义数据库连接函数的,up.asp用来上传图片,show.asp用来从数据库读取图片.

-------------conn.asp--------------
<%
dim conn,rs

function getdata(sql)
dbpath="img.mdb"
set conn=server.CreateObject("ADODB.connection")
connstr="provider=Microsoft.jet.oledb.4.0;data source="&server.MapPath(dbpath)
conn.open connstr
set rs=server.CreateObject("ADODB.recordset")
rs.open sql,conn,3,2
end function

sub rsclose()
rs.close()
set rs=nothing
conn.close()
set conn=nothing
end sub
%>

---------------up.asp---------------
<html>
<body>
<form action="up.asp" method="post" enctype="multipart/form-data" >
<input type="file" size="12" name="imgurl" id="imgurl">
<input type="submit" value="upload">
</form>
</body>
</html>
<%
if (request.totalbytes)>0 then '如果有数据提交,则进行下面的处理
%>
<!--#include file="conn.asp"-->
<%
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)
sql="select * from pic"
getdata(sql)
rs.addnew
rs("img").AppendChunk myData
rs.update
rsclose()
response.clear
response.write "success!"
end if
%>

-------------show.asp--------------
<!--#include file="conn.asp"-->
<%
id=trim(request("id"))
sql="select * from pic where id="&id
getdata(sql)
Response.ContentType="image/*"
Response.BinaryWrite rs("img").getChunk(8000000)
rsclose()
%>

图片上传后就可以通过show.asp?id=*来读取了,你也可以直接将图片用<img>标签插入其他页面中,如<img src="show.asp?id=1" />

不好意思,未做注释,有不懂的先到网上搜一下,不行再联系我吧

代码比较多,网上有这一套插件,你可以搜一下,很多

uploadtest.asp
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body bgcolor="#FFFFFF">
<form method="post" name="form1" enctype="multipart/form-data" action="showdata.asp">
<p>text1:
<input type="text" name="text1">
</p>
<p>text2:
<input type="text" name="text2">
</p>
<p>txtarea:
<textarea name="textfield" cols="20" rows="10"></textarea>
</p>
<p>file:
<input type="file" name="newfile">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">
</p>
</form>
</body>
</html>

文件:showdata.asp
<!--#INCLUDE FILE="upload.inc"-->
<%
'Fields("xxx").Name 取得Form中xxx(Form Object)的名字
'Fields("xxx").FilePath 如果是file Object 取得文件的完整路径
'Fields("xxx").FileName 如果是file Object 取得文件名
'Fields("xxx").ContentType 如果是file Object 取得文件的类型
'Fields("xxx").Length 取得Form中xxx(Form Object)的数据长度
'Fields("xxx").Value 取得Form中xxx(Form Object)的数据内容
Dim FormData,FormSize
FormSize=Request.TotalBytes
FormData=Request.BinaryRead(FormSize)
Set Fields = GetUpload(FormData)
response.write "text1:" & Fields("text1").Value & "<br>" & VbCrLf
response.write "text2:" & Fields("text2").Value & "<br>" & VbCrLf
response.write "textarea:" & Fields("textfield").Value & "<br>" & VbCrLf
response.write Fields("newfile").FileName
response.write Fields("newfile").ContentType
Response.ContentType = Fields("newfile").ContentType
If Fields("newfile").FileName<>"" Then
Response.ContentType = Fields("newfile").ContentType
response.binarywrite Fields("newfile").Value
End If

'Response.BinaryWrite FormData
%>