Thank you, Bardo,不知你是不是打开文件,读到一个变量中,再利用appendchunk,如果是这样 的话,我用open + 文件名 for binary as ... 用lof求文件字节数为64000,可只读到6字节 就报告超出文件结尾,我不知怎么回事,我对Print,Put,Write,Get 等搞不懂,请你给我个例子 行吗?
Dim S() as Byte Dim logFileLen as Long ‘文件长度 logFileLen=FileLen("C:\aaa.doc") Redim S(logFileLen-1) Open "C:\aaa.doc" For Binary as #1 Get #1,,S ’中间一个参数是起始位置省略 Close #1最后S数组里就是这个文件了。
Dim SheetData() As Byte Dim strFile As String Dim bkA As Variant
strFile = 文件路径 Open strFile For Binary As #1 SheetData = InputB(LOF(1), #1) Close #1
一个可用的例子:ADO 存 Private Sub frmMain_NewPic(PicFileName As String) If theModel.id <> -1 Then Dim FileNumber FileNumber = FreeFile Open PicFileName For Binary Access Read As #FileNumber ReDim pic(LOF(FileNumber)) As Byte Get #FileNumber, , pic Dim T() As String T = Split(PicFileName, "\") Call thePic.SetPicture(pic, theModel.id, T(UBound(T))) ReDim pic(1) As Byte Close #FileNumber End If
End SubPublic Function SetPicture(ByRef pic() As Byte, ParentID As Long, FileName As String) On Error GoTo SetPictureErr '写入库 If UBound(pic) > 1000000 Then '太大乐 SetPicture = False Exit Function End If With mStorage .QueryString = "select maxpicid from maxid" .Execute mvarID = .Recordset!maxpicid .QueryString = "insert into pic (id,parentid,pictype,FileName)values(" & CStr(Me.id) & "," & CStr(ParentID) & ",'model','" & FileName & "')" .Execute .QueryString = "update maxid set maxpicid=maxpicid+1" .Execute .QueryString = "select pic from pic where id=" & Me.id Call .Execute(adOpenDynamic, adLockOptimistic)
.Recordset.Fields("pic").AppendChunk pic .Recordset.Update .CloseRecordset End With
Exit Function SetPictureErr: mStorage.CloseRecordset Select Case Err.Number 'case Case Else Call ErrLog(TypeName(Me) & ".SetPicture", "未捕获的错误") Call RaiseError(MyUnhandledError, TypeName(Me) & ".SetPicture") End Select End Function取 Private Function GetPicFile(ByRef pic() As Byte, FileName As String) As String Dim FileNumber FileNumber = FreeFile Open FileName For Binary Access Write As #FileNumber Dim tmp() As Byte Dim fz As Long fz = UBound(pic) If fz <> -1 Then ReDim tmp(fz) '强制成byte数组,避免put写入类型头 tmp = pic '写 Put #FileNumber, , tmp Close #FileNumber
End If GetPicFile = FileName End FunctionPublic Function GetPicture() As Byte() On Error GoTo GetPictureErr Dim mPicture() As Byte
With mStorage .QueryString = "select pic from pic where id=" & Me.id .Execute (adOpenStatic) If .RecordsExists Then If .Recordset.Fields("pic").ActualSize = 0 Then ReDim mPicture(1) As Byte GetPicture = mPicture Else ReDim mPicture(.Recordset.Fields("pic").ActualSize) As Byte mPicture = .Recordset.Fields("pic") GetPicture = mPicture ReDim mPicture(1) As Byte End If Else '图像不存在 'todo End If .CloseRecordset End With
Exit Function GetPictureErr: mStorage.CloseRecordset Select Case Err.Number 'case Case Else Call ErrLog(TypeName(Me) & ".GetPicture", "未捕获的错误") Call RaiseError(MyUnhandledError, TypeName(Me) & ".GetPicture") End SelectEnd Function
的话,我用open + 文件名 for binary as ... 用lof求文件字节数为64000,可只读到6字节
就报告超出文件结尾,我不知怎么回事,我对Print,Put,Write,Get 等搞不懂,请你给我个例子
行吗?
Dim logFileLen as Long ‘文件长度
logFileLen=FileLen("C:\aaa.doc")
Redim S(logFileLen-1)
Open "C:\aaa.doc" For Binary as #1
Get #1,,S ’中间一个参数是起始位置省略
Close #1最后S数组里就是这个文件了。
Dim SheetData() As Byte
Dim strFile As String
Dim bkA As Variant
strFile = 文件路径
Open strFile For Binary As #1
SheetData = InputB(LOF(1), #1)
Close #1
DataTQL.Recordset.Edit
bkA = DataTQL.Recordset.Book
DataTQL.Recordset.Fields(2) = SheetData
DataTQL.Recordset.Update
txtFile.Text = "存在Excel文件"
DataTQL.Recordset.Book = bkA'类似于这样,字段的属性是二进制型
存
Private Sub frmMain_NewPic(PicFileName As String)
If theModel.id <> -1 Then
Dim FileNumber
FileNumber = FreeFile
Open PicFileName For Binary Access Read As #FileNumber
ReDim pic(LOF(FileNumber)) As Byte
Get #FileNumber, , pic
Dim T() As String
T = Split(PicFileName, "\")
Call thePic.SetPicture(pic, theModel.id, T(UBound(T)))
ReDim pic(1) As Byte
Close #FileNumber
End If
End SubPublic Function SetPicture(ByRef pic() As Byte, ParentID As Long, FileName As String)
On Error GoTo SetPictureErr
'写入库
If UBound(pic) > 1000000 Then
'太大乐
SetPicture = False
Exit Function
End If
With mStorage
.QueryString = "select maxpicid from maxid"
.Execute
mvarID = .Recordset!maxpicid
.QueryString = "insert into pic (id,parentid,pictype,FileName)values(" & CStr(Me.id) & "," & CStr(ParentID) & ",'model','" & FileName & "')"
.Execute
.QueryString = "update maxid set maxpicid=maxpicid+1"
.Execute
.QueryString = "select pic from pic where id=" & Me.id
Call .Execute(adOpenDynamic, adLockOptimistic)
.Recordset.Fields("pic").AppendChunk pic
.Recordset.Update
.CloseRecordset
End With
Exit Function
SetPictureErr:
mStorage.CloseRecordset Select Case Err.Number
'case
Case Else
Call ErrLog(TypeName(Me) & ".SetPicture", "未捕获的错误")
Call RaiseError(MyUnhandledError, TypeName(Me) & ".SetPicture")
End Select
End Function取
Private Function GetPicFile(ByRef pic() As Byte, FileName As String) As String
Dim FileNumber
FileNumber = FreeFile
Open FileName For Binary Access Write As #FileNumber
Dim tmp() As Byte
Dim fz As Long
fz = UBound(pic)
If fz <> -1 Then
ReDim tmp(fz)
'强制成byte数组,避免put写入类型头
tmp = pic
'写
Put #FileNumber, , tmp
Close #FileNumber
End If
GetPicFile = FileName
End FunctionPublic Function GetPicture() As Byte()
On Error GoTo GetPictureErr
Dim mPicture() As Byte
With mStorage
.QueryString = "select pic from pic where id=" & Me.id
.Execute (adOpenStatic)
If .RecordsExists Then
If .Recordset.Fields("pic").ActualSize = 0 Then
ReDim mPicture(1) As Byte
GetPicture = mPicture
Else
ReDim mPicture(.Recordset.Fields("pic").ActualSize) As Byte
mPicture = .Recordset.Fields("pic")
GetPicture = mPicture
ReDim mPicture(1) As Byte
End If
Else
'图像不存在
'todo End If
.CloseRecordset
End With
Exit Function
GetPictureErr:
mStorage.CloseRecordset
Select Case Err.Number
'case
Case Else
Call ErrLog(TypeName(Me) & ".GetPicture", "未捕获的错误")
Call RaiseError(MyUnhandledError, TypeName(Me) & ".GetPicture")
End SelectEnd Function
谢谢 lijunfeng(我是我) 谢谢 feihong0233(泓) 谢谢 panhwa(tn't)