二进制存取,显示时需要改变HTTP-CONTENT

解决方案 »

  1.   

    读出:       Dim bit1() As Byte
            On Error GoTo Err1:
            bit1 = r("zp").GetChunk(r("zp").ActualSize)
            '然后将字节数组的内容拼装成文件即可
            Open App.Path & "\temp.jpg" For Binary As #1
            Put 1, 1, bit1
            Close 1
        Image1.Stretch = False
        Image1 = LoadPicture(App.Path & "\temp.jpg")
    写入:        Open  图片路径和文件名  For Binary As #1
            ReDim bit(LOF(1)) As Byte
            Get 1, 1, bit
            Close 1
            r.Open "select * from byzl where kh='" & Trim(Txtkh) & "'", Cn, adOpenKeyset, adLockOptimistic
            r("zp").AppendChunk bit
            r.Update
            r.Close 
      

  2.   

    使用dao存储:
    dim appendok as boolean
        rs.edit    VB.SavePicture Picture1.Picture("c:\me.jpg")      appendok=DAOAppendBlobFromFile(rs!blobfield,"c:\me.jpg")
        if appendok then 
          rs.update
        else
          rs.cancelupdate
        end if 
    :读取:
        dim readok as boolean 
            readok=DAOGetBlobToFile(rs!blobfield,"c:\me.jpg")        picture1.Picture=vb.LoadPicture("c:\me.jpg")  '读取放到PICTUREBOx
    Public Function DAOAppendBlobFromFile(blobColumn As Field, ByVal FileName) As Boolean
    Dim FileNumber As Integer, DataLen As Long
    Dim Chunks As Long, ChunkAry() As Byte
    Dim ChunkSize As Long, Fragment As Long, i As Long
    Dim str5 As String
    On Error GoTo errh:
    AppendBlobFromFile = False
    ChunkSize = 2048FileNumber = FreeFile
    Open FileName For Binary Access Read As FileNumber
    DataLen = LOF(FileNumber)      '文件中数据的长度
    If DataLen = 0 Then
        Close FileNumber
        DAOAppendBolbFromFile = True
        Exit Function
    End If
    Chunks = DataLen \ ChunkSize
    Fragment = DataLen Mod ChunkSize
    If Fragment > 0 Then
        ReDim ChunkAry(Fragment - 1)
        Get FileNumber, , ChunkAry()
        blobColumn.AppendChunk ChunkAry
    End If
    ReDim ChunkAry(ChunkSize - 1)
    For i = 1 To Chunks
        Get FileNumber, , ChunkAry()
        blobColumn.AppendChunk ChunkAry
    Next i
    Close FileNumber
    DAOAppendBlobFromFile = True
    Exit Function
    errh:
        DAOAppendBlobFromFile = False
        MsgBox Err.Description, vbCritical, "AppendBlobFromFile错误!!"
    End Function'以下
    '把Blob的栏位内的数据读出来,放到某个File之内
    Public Function DAOGetBlobToFile(blobColumn As Field, ByVal FileName As String) As Boolean
    Dim FileNumber As Integer, DataLen As Long
    Dim Chunks As Long, ChunkAry() As Byte
    Dim ChunkSize As Long, Fragment As Long, i As Long
    On Error GoTo errh:
    ADOGetBlobToFile = False
    If IsNull(blobColumn) Then Exit FunctionDataLen = blobColumn.FieldSize        ' 文件中数据的长度
    If DataLen = 0 Then
        DAOGetBlobToFile = False
    '里面没有数据,而不是Null
    Exit Function
    End If
    ChunkSize = 2048FileNumber = FreeFile
    Open FileName For Binary Access Write As FileNumberChunks = DataLen \ ChunkSize
    Fragment = DataLen Mod ChunkSize
    If Fragment > 0 Then
        ReDim ChunkAry(Fragment - 1)
        ChunkAry = blobColumn.GetChunk(0, Fragment)
        Put FileNumber, , ChunkAry
    End If
    ReDim ChunkAry(ChunkSize - 1)
    For i = 1 To Chunks
        ChunkAry = blobColumn.GetChunk(Fragment + (i - 1) * ChunkSize, ChunkSize)
        Put FileNumber, , ChunkAry
    Next
    Close FileNumber
    DAOGetBlobToFile = True
    Exit Function
    errh:
        DAOGetBlobToFile = False
        MsgBox Err.Description, vbCritical, "ADOGetBlobToFile错误!!"
    End Function