- 深入解析ASP核心技術(shù)
- 王洪影
- 395字
- 2019-01-03 18:15:59
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對象的一大用途。
推薦閱讀
- Java Web程序開發(fā)范例寶典
- 一線架構(gòu)師實(shí)踐指南
- 現(xiàn)代C++軟件架構(gòu):方法與實(shí)踐
- ODPS權(quán)威指南 阿里大數(shù)據(jù)平臺應(yīng)用開發(fā)實(shí)踐
- 從缺陷中學(xué)習(xí)C/C++
- SQL Server應(yīng)用與開發(fā)范例寶典
- Swift開發(fā)實(shí)戰(zhàn)
- 這就是MCP
- AIDevOps:智能微服務(wù)開發(fā)、運(yùn)維原理與實(shí)踐
- Swift從入門到精通(正式版)
- Verilog HDL數(shù)字系統(tǒng)設(shè)計及實(shí)踐
- Unity手機(jī)游戲開發(fā):從搭建到發(fā)布上線全流程實(shí)戰(zhàn)
- 現(xiàn)代API:通往架構(gòu)師之門
- 大話軟件工程:需求分析與軟件設(shè)計
- 鋒利的jQuery