各位:通常我们保存附件是通过传二文件路径来保存的,但现在用户传过来的是附件的二进制数,请问有办法吗?如下是路径的方法:
'保存相片
Private Function SendImageData(conn As Connection, ByVal SqlString As String, ByVal filename As String) As Integer
    Dim rs As New Recordset
    Const ChunkSize = 163840
    Dim Chunk() As Byte
    Dim Chunks As Long
    Dim Fragment As Long
    Dim FL As Long
    Dim I As Long
    Dim filenum As Integer
    On Error GoTo WriteErr
    filenum = FreeFile
    Open filename For Binary Access Read As #filenum
    FL = LOF(filenum)
    If FL = 0 Then
        SendImageData = 0
        Exit Function
    End If
    Chunks = FL \ ChunkSize
    Fragment = FL Mod ChunkSize
    
    rs.CursorType = adOpenKeyset
    rs.LockType = adLockOptimistic
    rs.Open SqlString, conn
    
    If rs.EOF Then GoTo WriteErr
    
    If Fragment > 0 Then
        ReDim Chunk(Fragment - 1)
        Get filenum, , Chunk
        rs(1).AppendChunk Chunk
    End If
    ReDim Chunk(ChunkSize - 1)
    For I = 1 To Chunks
        Get filenum, , Chunk
        rs(1).AppendChunk Chunk
    Next I
    rs.Update
    Close #filenum
    SendImageData = 1
    Exit Function
WriteErr:
    SendImageData = 0
End Function

解决方案 »

  1.   

    更简单,直接拿数组进行 AppendChunk
    Private Function SendImageData(conn As Connection, ByVal SqlString As String, ByRef data() As Byte) As Integer
        Dim rs As New Recordset
        Dim FL As Long
        On Error GoTo WriteErr
        
        FL = UBound(data) - LBound(data) + 1
        If FL = 0 Then
            SendImageData = 0
            Exit Function
        End If
        
        rs.CursorType = adOpenKeyset
        rs.LockType = adLockOptimistic
        rs.Open SqlString, conn
        
        If rs.EOF Then GoTo WriteErr
        
        rs(1).AppendChunk data
        
        rs.Update
        SendImageData = 1
        Exit Function
    WriteErr:
        SendImageData = 0
    End Function