欢迎光临
我们一直在努力

无组件上传图片至SQLSERVER数据库-ASP教程,数据库相关

建站超值云服务器,限时71元/月

在无组件上传我刚试成功,所以提供代码给大家一起共享。

/* addemployee.asp */

<html>

<head>

<title>职工之家</title>

<meta http-equiv="content-type" content="text/html; charset=gb2312">

<link rel="stylesheet" href="../css/site_css.css" type="text/css">

</head>

<script language="javascript">

<!–

//选择分类

///////////////////////////////////////////////////////////////////////

function selectsort(txtsubject){

var returnvalue

returnvalue=window.showmodaldialog("selmode.htm",null,"center:1;status:0;help:0;resized:0;dialogheight:300px;dialogwidth:206px");

if (returnvalue!="" && returnvalue!=null){

txtsubject.value=returnvalue

}

}

///////////////////////////////////////////////////////////////////////

//合法性检查

function isok(thisform){

var strtemp,strvalue,strlen,strexname

 if(thisform.txttitle.value==""){

alert("提示:标题不能为空,请正确输入")

thisform.txttitle.focus()

return false

}

if(thisform.txtsort.value==""){

alert("提示:请正确选择分类")

thisform.txtsort.focus()

return false

}

/*检查图片类型*/

if(thisform.file.value!=""){

strtemp=thisform.file.value

strvalue=strtemp.tolowercase()

strlen=strtemp.length

strexname=strvalue.substring(strlen-4,strlen)

if (strexname!=".jpg" && strexname!=".gif"){

alert("请选择jpg或者gif文件!")

return false

}

return true

}

}

//–>

</script>

<body bgcolor="#ffffff" text="#000000" leftmargin="1" topmargin="1">

<form name="form1" method="post" action="transact1.asp" enctype="multipart/form-data">

<table border="0" cellspacing="0" cellpadding="0">

<tr>

<td colspan="2" bgcolor="#006699" height="15">&nbsp;</td>

</tr>

<tr>

<td class="textblack">

<div align="right">标题:</div>

</td>

<td>

<input type="text" name="txttitle" size="52" class="textarea">

</td>

</tr>

<tr>

<td class="textblack">

<div align="right">分类:</div>

</td>

<td>

<input type="text" name="txtsort" size="35" class="textarea">

<input type="button" name="submit2" class="buttonskid" onclick="selectsort(txtsort);">

</td>

</tr>

<tr>

<td class="textblack" valign="top">

<div align="right">正文:</div>

</td>

<td>

<textarea name="txtcontent" rows="15" cols="50" class="textarea"></textarea>

</td>

</tr>

<tr>

<td height="14" class="textblack" valign="top">

<div align="right">图片:</div>

</td>

<td height="14" class="textblack">

<div align="left">

<input type="file" name="file" size="35" class="textarea">

</div>

</td>

</tr>

<tr>

<td height="42" class="textblack" valign="top">&nbsp;</td>

<td height="42" class="textblack" valign="middle">

<p>1、您上传的图片大小请控制在<font color="#ff0000"><b>500k</b></font>以内,否则不允许上传<br>

2、你上传的图片必须为<font color="#ff0000"><b>150*130象素</b></font><br>

3、上传上传图片的格式为jpg或者gif</p>

</td>

</tr>

<tr>

<td height="39" class="textblack">&nbsp;</td>

<td height="39" valign="middle">

<div align="center"><img src="../images/save.gif" width="85" height="19" onclick="if(isok(form1)){form1.submit()}">  

<img src="../images/close.gif" width="85" height="19" onclick="self.close();" ></div>

</td>

</tr>

</table>

</form>

</body>

</html>

*************************************************************************

/* transact1.asp*/

<!–#include file="../func/conn.inc"–>

<!–#include file="../func/fupload.inc"–>

<!–#include file ="../func/myfunctions.inc"–>

<%

if request.servervariables("request_method") = "post" then

dim fields

dim strtitle,strsort,strcontent

dim rs,ssql

dim imaxid

dim strmaxid

dim strlen

set fields = getupload()

strtitle=binarytostring(fields("txttitle").value)

strsort=binarytostring(fields("txtsort").value)

strcontent=binarytostring(fields("txtcontent").value)

strsort=split(trim(strsort),"-")

if instr(1,lcase(fields("file").filename),".jpg")=0 and instr(1,lcase(fields("file").filename),".gif")=0 then

response.write "<script language=javascript>alert(上传的图片必须是gif或者jpg格式的图片)</script>"

response.write "<script language=javascript>window.location=addemployee.asp;</script>"

response.end

end if

if fields("file").length>500000 then

response.write "<script language=javascript>alert(只允许不大于500k的图片上传);</script>"

response.write "<script language=javascript>window.location=addemployee.asp;</script>"

response.end

end if

/*存至数据库*/

if fields("file").filename<>"" then

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

ssql="select * from employee order by id desc"

rs.open ssql,conn,2,2

if not rs.eof then

imaxid=clng(rs("id"))+1

strlen=4-len(cstr(imaxid))

strmaxid=string(strlen,"0") & cstr(imaxid)

else

strmaxid="0001"

end if

rs.addnew

rs("id")=strmaxid

rs("title")=strtitle

rs("sort")=strsort(0)

rs("img").appendchunk fields("file").value

rs("content")=quotechg(strcontent)

rs("todate")=date()

rs.update

rs.close

response.write "<script language=javascript>alert(添加记录成功)</script>"

end if

end if

%>

**********************************************************************

/*fupload.inc*/

<script runat=server language=vbscript>

dim uploadsizelimit

********************************** getupload **********************************

.name name of the form field (<input name="…" type="file,…">)

.contentdisposition = content-disposition of the form field

.filename = source file name for <input type=file>

.contenttype = content-type for <input type=file>

.value = binary value of the source field.

.length = len of the binary data field

function getupload()

dim result

set result = nothing

if request.servervariables("request_method") = "post" then request method must be "post"

dim ct, posb, boundary, length, pose

ct = request.servervariables("http_content_type") reads content-type header

response.write ct

application/x-www-form-urlencoded

if lcase(left(ct, 19)) = "multipart/form-data" then content-type header must be "multipart/form-data"

this is upload request.

get the boundary and length from content-type header

posb = instr(lcase(ct), "boundary=") finds boundary

if posb > 0 then boundary = mid(ct, posb + 9) separetes boundary

length = clng(request.servervariables("http_content_length")) get content-length header

if "" & uploadsizelimit<>"" then

uploadsizelimit = clng(uploadsizelimit)

if length > uploadsizelimit then

on error resume next clears the input buffer

response.addheader "connection", "close"

on error goto 0

request.binaryread(length)

err.raise 2, "getupload", "upload size " & formatnumber(length,0) & "b exceeds limit of " & formatnumber(uploadsizelimit,0) & "b"

exit function

end if

end if

if length > 0 and boundary <> "" then are there required informations about upload ?

boundary = "–" & boundary

dim head, binary

binary = request.binaryread(length) reads binary data from client

retrieves the upload fields from binary data

set result = separatefields(binary, boundary)

binary = empty clear variables

else

err.raise 10, "getupload", "zero length request ."

end if

else

err.raise 11, "getupload", "no file sent."

end if

else

err.raise 1, "getupload", "bad request method."

end if

set getupload = result

end function

********************************** separatefields **********************************

this function retrieves the upload fields from binary data and retuns the fields as array

binary is safearray of all raw binary data from input.

function separatefields(binary, boundary)

dim posopenboundary, poscloseboundary, posendofheader, islastboundary

dim fields

boundary = stringtobinary(boundary)

posopenboundary = instrb(binary, boundary)

poscloseboundary = instrb(posopenboundary + lenb(boundary), binary, boundary, 0)

set fields = createobject("scripting.dictionary")

do while (posopenboundary > 0 and poscloseboundary > 0 and not islastboundary)

header and file/source field data

dim headercontent, fieldcontent

header fields

dim content_disposition, formfieldname, sourcefilename, content_type

helping variables

dim field, twocharsafterendboundary

get end of header

posendofheader = instrb(posopenboundary + len(boundary), binary, stringtobinary(vbcrlf + vbcrlf))

separates field header

headercontent = midb(binary, posopenboundary + lenb(boundary) + 2, posendofheader – posopenboundary – lenb(boundary) – 2)

separates field content

fieldcontent = midb(binary, (posendofheader + 4), poscloseboundary – (posendofheader + 4) – 2)

separates header fields from header

getheadfields binarytostring(headercontent), content_disposition, formfieldname, sourcefilename, content_type

create one field and assign parameters

set field = createuploadfield()

field.name = formfieldname

field.contentdisposition = content_disposition

field.filepath = sourcefilename

field.filename = getfilename(sourcefilename)

field.contenttype = content_type

field.value = fieldcontent

field.length = lenb(fieldcontent)

fields.add formfieldname, field

is this ending boundary ?

twocharsafterendboundary = binarytostring(midb(binary, poscloseboundary + lenb(boundary), 2))

binary.mid(poscloseboundary + len(boundary), 2).string

islastboundary = twocharsafterendboundary = "–"

if not islastboundary then this is not ending boundary – go to next form field.

posopenboundary = poscloseboundary

poscloseboundary = instrb(posopenboundary + lenb(boundary), binary, boundary )

end if

loop

set separatefields = fields

end function

********************************** utilities **********************************

function binarytostring(str)

strto = ""

for i=1 to lenb(str)

if ascb(midb(str, i, 1)) > 127 then

strto = strto & chr(ascb(midb(str, i, 1))*256+ascb(midb(str, i+1, 1)))

i = i + 1

else

strto = strto & chr(ascb(midb(str, i, 1)))

end if

next

binarytostring=strto

end function

function stringtobinary(string)

dim i, b

for i=1 to len(string)

b = b & chrb(asc(mid(string,i,1)))

next

stringtobinary = b

end function

separates header fields from upload header

function getheadfields(byval head, content_disposition, name, filename, content_type)

content_disposition = ltrim(separatefield(head, "content-disposition:", ";"))

name = (separatefield(head, "name=", ";")) ltrim

if left(name, 1) = """" then name = mid(name, 2, len(name) – 2)

filename = (separatefield(head, "filename=", ";")) ltrim

if left(filename, 1) = """" then filename = mid(filename, 2, len(filename) – 2)

content_type = ltrim(separatefield(head, "content-type:", ";"))

end function

separets one filed between sstart and send

function separatefield(from, byval sstart, byval send)

dim posb, pose, sfrom

sfrom = lcase(from)

posb = instr(sfrom, sstart)

if posb > 0 then

posb = posb + len(sstart)

pose = instr(posb, sfrom, send)

if pose = 0 then pose = instr(posb, sfrom, vbcrlf)

if pose = 0 then pose = len(sfrom) + 1

separatefield = mid(from, posb, pose – posb)

else

separatefield = empty

end if

end function

separetes file name from the full path of file

function getfilename(fullpath)

dim pos, posf

posf = 0

for pos = len(fullpath) to 1 step -1

select case mid(fullpath, pos, 1)

case "/", "\": posf = pos + 1: pos = 0

end select

next

if posf = 0 then posf = 1

getfilename = mid(fullpath, posf)

end function

</script>

<script runat=server language=jscript>

//the function creates field object.

function createuploadfield(){ return new uf_init() }

function uf_init(){

this.name = null

this.contentdisposition = null

this.filename = null

this.filepath = null

this.contenttype = null

this.value = null

this.length = null

}

</script>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 无组件上传图片至SQLSERVER数据库-ASP教程,数据库相关
分享到: 更多 (0)

相关推荐

  • 暂无文章