六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

中文的無組件文件上傳ASP函數

[摘要]說明:持中文的無組件文件上傳ASP函數,由于ASP不支持二進制寫入文件,所以存成文件時必須使用組件,本函數只提供截取上傳文件的數據,可以寫入到數據庫。<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>Function GetUpload(FormData) ...
說明:持中文的無組件文件上傳ASP函數,由于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

'把二進制字符串轉換成普通字符串函數
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
}

示例

【woozhj】 于 00-4-17 上午 09:37:43 加貼在 Joy ASP ↑:

文件: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
    %>

wakeful】 于 00-4-15 下午 08:30:49 加貼在 Joy ASP ↑:
file:upload.asp
<%
' Author Philippe Collignon
' Email [email protected]

Sub BuildUploadRequest(RequestBin)
    'Get the boundary
    PosBeg = 1
    PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
    boundary = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
    boundaryPos = InstrB(1,RequestBin,boundary)
    'Get all data inside the boundaries
    Do until (boundaryPos=InstrB(RequestBin,boundary & getByteString("--")))
        'Members variable of objects are put in a dictionary object
        Dim UploadControl
        Set UploadControl = CreateObject("Scripting.Dictionary")
        'Get an object name
        Pos = InstrB(BoundaryPos,RequestBin,getByteString("Content-Disposition"))
        Pos = InstrB(Pos,RequestBin,getByteString("name="))
        PosBeg = Pos+6
        PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
        Name = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
        PosFile = InstrB(BoundaryPos,RequestBin,getByteString("filename="))
        PosBound = InstrB(PosEnd,RequestBin,boundary)
        'Test if object is of file type
        If  PosFile<>0 AND (PosFile<PosBound) Then
            'Get Filename, content-type and content of file
            PosBeg = PosFile + 10
            PosEnd =  InstrB(PosBeg,RequestBin,getByteString(chr(34)))
            FileName = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
            'Add filename to dictionary object
            UploadControl.Add "FileName", FileName
            Pos = InstrB(PosEnd,RequestBin,getByteString("Content-Type:"))
            PosBeg = Pos+14
            PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
            'Add content-type to dictionary object
            ContentType = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
            UploadControl.Add "ContentType",ContentType
            'Get content of object
            PosBeg = PosEnd+4
            PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
            Value = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
            Else
            'Get content of object
            Pos = InstrB(Pos,RequestBin,getByteString(chr(13)))
            PosBeg = Pos+4
            PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
            Value = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
        End If
        'Add content to dictionary object
    UploadControl.Add "Value" , Value
        'Add dictionary object to main dictionary
    UploadRequest.Add name, UploadControl
        'Loop to next object
        BoundaryPos=InstrB(BoundaryPos+LenB(boundary),RequestBin,boundary)
    Loop

End Sub

'String to byte string conversion
Function getByteString(StringStr)
For i = 1 to Len(StringStr)
     char = Mid(StringStr,i,1)
    getByteString = getByteString & chrB(AscB(char))
Next
End Function

'Byte string to string conversion
Function getString(StringBin)
getString =""
For intCount = 1 to LenB(StringBin)
    getString = getString & chr(AscB(MidB(StringBin,intCount,1)))
Next
End Function
%>

outputfile.asp

<%
' Author Philippe Collignon
' Email [email protected]


Response.Expires=0
Response.Buffer = TRUE
Response.Clear
'Response.BinaryWrite(Request.BinaryRead(Request.TotalBytes))
byteCount = Request.TotalBytes
'Response.BinaryWrite(Request.BinaryRead(varByteCount))

RequestBin = Request.BinaryRead(byteCount)
Dim UploadRequest
Set UploadRequest = CreateObject("Scripting.Dictionary")

BuildUploadRequest  RequestBin

email = UploadRequest.Item("email").Item("Value")

contentType = UploadRequest.Item("blob").Item("ContentType")
filepathname = UploadRequest.Item("blob").Item("FileName")
filename = Right(filepathname,Len(filepathname)-InstrRev(filepathname,"\"))
value = UploadRequest.Item("blob").Item("Value")

'Create FileSytemObject Component
Set ScriptObject = Server.CreateObject("Scripting.FileSystemObject")

'Create and Write to a File
pathEnd = Len(Server.mappath(Request.ServerVariables("PATH_INFO")))-14
Set MyFile = ScriptObject.CreateTextFile(Left(Server.mappath(Request.ServerVariables("PATH_INFO")),pathEnd)&"uploaded"&filename)

For i = 1 to LenB(value)
     MyFile.Write chr(AscB(MidB(value,i,1)))
Next

MyFile.Close
%>
<b>Uploaded file : </b><%="uploaded"&filename%><BR>
<img src="<%="uploaded"&filename%>">
<!--#include file="upload.asp"-->

outputclient.asp

<%
' Author Philippe Collignon
' Email [email protected]

Response.Buffer = TRUE
Response.Clear

byteCount = Request.TotalBytes

RequestBin = Request.BinaryRead(byteCount)
Dim UploadRequest
Set UploadRequest = CreateObject("Scripting.Dictionary")

BuildUploadRequest  RequestBin

email = UploadRequest.Item("email").Item("Value")

contentType = UploadRequest.Item("blob").Item("ContentType")
filepathname = UploadRequest.Item("blob").Item("FileName")
filename = Right(filepathname,Len(filepathname)-InstrRev(filepathname,"\"))
value = UploadRequest.Item("blob").Item("Value")
%>

Your email is : <%= email %><BR>
File name of you picture is <%=filepathname%><BR>
File type of your picture is <%=contentType%><BR>

<!--#include file="upload.asp"-->
binaryoutputclient.asp

<%
' Author Philippe Collignon
' Email [email protected]

Response.Buffer = TRUE
Response.Clear
byteCount = Request.TotalBytes

RequestBin = Request.BinaryRead(byteCount)
Dim UploadRequest
Set UploadRequest = CreateObject("Scripting.Dictionary")

BuildUploadRequest  RequestBin

email = UploadRequest.Item("email").Item("Value")

contentType =  UploadRequest.Item("blob").Item("ContentType")
filepathname = UploadRequest.Item("blob").Item("FileName")
filename = Right(filepathname,Len(filepathname)-InstrRev(filepathname,"\"))
picture = UploadRequest.Item("blob").Item("Value")

Response.ContentType = contentType
Response.binaryWrite picture

%>
<!--#include file="upload.asp"-->
uploadform.htm

<!-- Author Philippe Collignon -->
<!-- Email [email protected] -->
<HTML>
<HEAD>
    <TITLE>Upload Form</TITLE>
</HEAD>
<BODY>
<B>Output to client</b>
<FORM METHOD="Post" ENCTYPE="multipart/form-data" ACTION="outputClient.asp">
Email : <INPUT TYPE="Text" NAME="email" VALUE="[email protected]"><BR>
Picture : <INPUT TYPE="file" NAME="blob"><BR>
<INPUT TYPE="submit" NAME="Enter">
</FORM>
<B>Binary output to client</b>
<FORM METHOD="Post" ENCTYPE="multipart/form-data" ACTION="binaryOutputClient.asp">
Email : <INPUT TYPE="Text" NAME="email" VALUE="[email protected]"><BR>
Picture : <INPUT TYPE="file" NAME="blob"><BR>
<INPUT TYPE="submit" NAME="Enter">
</FORM>
<B>Output to file system</b>
<FORM METHOD="Post" ENCTYPE="multipart/form-data" ACTION="outputFile.asp">
Email : <INPUT TYPE="Text" NAME="email" VALUE="[email protected]"><BR>
Picture : <INPUT TYPE="file" NAME="blob"><BR>
<INPUT TYPE="submit" NAME="Enter">
</FORM>
</BODY>
</HTML>



主站蜘蛛池模板: 亚洲成年网 | 四虎国内精品一区二区 | 午夜影院a | 亚洲色视频在线播放网站 | 日本优女| 中文字幕在线视频精品 | 亚洲免费在线视频 | 在线观看 亚洲 | 日韩色图区 | 亚洲a成人7777777久久 | 色色色色网站 | 亚洲精品在线免费 | 日韩美女在线看免费观看 | 正在播放一区二区 | 欧美一级做a爰片免费 | 欧美一级片手机在线观看 | 天天射视频 | 日韩欧美亚洲综合 | 欧美又大粗又爽又黄大片视频黑人 | 天堂v亚洲国产v一区二区 | 亚洲婷婷六月 | 午夜影视在线观看免费完整高清大全 | 在线天堂中文在线资源网 | 中文在线最新版天堂8 | 日韩中文一区宇都宫紫苑 | 日日摸夜夜添夜夜添一区二区 | 伊人影院综合网 | 日本国产中文字幕 | 天堂资源8中文最新版在线 天堂中文字幕在线 | 四虎影院com| 日韩伦理| 日本污全彩肉肉无遮挡彩色 | 亚洲成人福利在线观看 | 日本免费黄视频 | 日韩高清在线日韩大片观看网址 | 日本精高清区一 | 色综合成人网 | 思思久久99| 日韩欧美一区二区三区不卡在线 | 在线观看欧美亚洲 | 夜夜操伊人 |