电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 技术学院
无组件上传图片至SQLSERVER数据库
作者:未知 点击:79
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 
在无组件上传我刚试成功,所以提供代码给大家一起共享。

/*  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>

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·MDAC2.8 下载!
·MySQL和SQLServer,到底选择谁?
·卖虚拟主机常见骗术
·国际域名知识信息注册全解
·让你的网站快速列入Google的最佳方法
·人站长应了解的最基础的知识
·PHP 和 MySQL 基础教程
·网站常用工具网站
·常用域名管理后台
·巧用百度BAIDU搜索电影

最新文章
·国内中文搜索实用攻略
·巧用百度BAIDU搜索电影
·网站排名:咱也权威一把
·专用小型搜索引擎
·搜出网上精彩
·实用网络搜索技巧
·用户搜索常见问题及解决方法小结
·如何查看自定义天数的ALEXA数据
·Alexa.com的排名原理
·内幕调查:出卖Alexa(下)




版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号