官术网_书友最值得收藏!

2.3.3 二進(jìn)制數(shù)據(jù)

寫入二進(jìn)制數(shù)據(jù)使用Write方法(即Type屬性為2時使用),就一個參數(shù),就是要寫入的數(shù)據(jù)。讀取二進(jìn)制數(shù)據(jù)使用Read方法,參數(shù)是讀取的字節(jié)數(shù),省略則從當(dāng)前位置一直讀到流的末尾。

Write方法的參數(shù)要求是真正的二進(jìn)制數(shù)據(jù),使用ChrB方法拼接的二進(jìn)制字符串是不行的。下面的范例使用了Request.BinaryRead()方法,以二進(jìn)制方式讀取表單提交的內(nèi)容,它返回的正是一個字節(jié)數(shù)組。

StreamBinaryData.asp

<%@codepage=936%>
<! --#include File="getMemoryFormat.asp" -->
<% Response.Charset="GBK" %>

<form method="post">
<input type="text" name="inputText" value="編碼">
<input type="submit" value="Go">
</form>

<%
If Request.TotalBytes > 0 Then
    Dim byteArray
    byteArray = Request.BinaryRead(Request.TotalBytes)
    response.write TypeName(byteArray) & "<br>"
    response.write VarType(byteArray) & "<br>"
    response.write "接收的數(shù)據(jù):" & getMemoryFormat(byteArray) & "<br>"

    Dim stream
    Set stream = Server.CreateObject("ADODB.Stream")
    stream.Type = 1                          '二進(jìn)制方式
    stream.Open
    stream.Write byteArray                   '寫入二進(jìn)制數(shù)據(jù)

    stream.Position = 0                      '移動指針到位置0
    response.write "流中的數(shù)據(jù):" & getMemoryFormat(stream.Read) & "<br>"

    stream.Position = 0                      '移動指針到位置0
    stream.Type = 2                          '變更為文本方式
    stream.Charset="GBK"
    response.write stream.ReadText           '輸出文本

    stream.close
    Set stream = nothing
End If %>

運(yùn)行結(jié)果如圖2-18所示。

圖2-18 二進(jìn)制數(shù)據(jù)處理范例

從運(yùn)行結(jié)果可以看出,二進(jìn)制數(shù)據(jù)是原樣寫入的,沒有任何變化。

此例中,我們先寫入二進(jìn)制數(shù)據(jù),然后變更為文本方式,按GBK編碼讀取文本,從而實(shí)現(xiàn)了二進(jìn)制數(shù)據(jù)到文本的轉(zhuǎn)換,這正是Stream對象的一大用途。

主站蜘蛛池模板: 阿合奇县| 瑞安市| 来宾市| 祥云县| 安图县| 武陟县| 乌拉特前旗| 抚松县| 达孜县| 高碑店市| 马边| 新田县| 新宾| 蒙城县| 洛宁县| 灵川县| 那曲县| 阳高县| 新巴尔虎左旗| 绥阳县| 鲁山县| 定安县| 晋中市| 义乌市| 开封市| 崇阳县| 图片| 高阳县| 枣强县| 台湾省| 六盘水市| 苍梧县| 榆树市| 房产| 申扎县| 玉树县| 永德县| 拉萨市| 岳西县| 讷河市| 潼关县|