【问题标题】:read attachment field from accdb with classic asp3/vbscript使用经典的 asp3/vbscript 从 accdb 读取附件字段
【发布时间】:2017-05-14 11:03:15
【问题描述】:

我的 accdb 数据库文件中有一个附件字段, 我正在尝试阅读它以提取附件,但它一直返回空值

录制到这个帖子Using Attachment field with Classic ASP adodb没有办法做到这一点,是真的吗?如果是,我还有什么其他方法可以做到这一点?

这是我正在运行的代码:

qid = request.querystring("qid")
wikiDbAddress="database/my.accdb"

set cnWiki=server.CreateObject("adodb.connection")  
cnWiki.open "DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};  DBQ=" & Server.MapPath(root&wikiDbAddress)

SQL = "select * from [Knowledge Base] where id="&qid

RS.Open SQL, cnWiki

do while not RS.eof 
    response.write RS("attachments")
    RS.movenext
loop

【问题讨论】:

  • 一些更新,经过一整天的尝试,这个查询带来了附件,但是,我仍然不知道如何序列化数据 SQL = "select Attachments.FileName as fname, Attachments.FileData作为数据,Attachments.FileType as FileType from [Knowledge Base] where id="&qid 有什么想法吗??

标签: ms-access vbscript asp-classic


【解决方案1】:

目前,我做了一些对我有帮助的解决方法,它效率不高,但确实有效。

qid = request.querystring("qid")
name = request.querystring("name")



SQL = "select Attachments.FileName as fname, Attachments.FileData as data, Attachments.FileType as FileType from [Knowledge Base] where Attachments.FileName='"&name&"' and id="&qid

RS.Open SQL, cnWiki


do while not RS.eof 
    if rs("fname")= name then   
        filename = Server.MapPath("/KB_"&qid&"_"&rs("fname"))
        set fs=Server.CreateObject("Scripting.FileSystemObject")
        if not fs.FileExists(filename) then
            SaveBinaryData filename, rs("data") 
            data = readBinary(filename)
            ' CHR(255) = FF, CHR(170) = AA
            data = Mid(data, 21, Len(data) - 20)
            writeBinary data,filename                
        end if
        set fs=nothing                              
        downloadFromFile(filename )
        exit do
    else        
        RS.movenext
    end if
loop
rs.close
cnWiki.close
function downloadFromFile(strFile )
    Dim objConn
    Dim intCampaignRecipientID

    If strFile <> "" Then           
        Dim objStream
        Set objStream = Server.CreateObject("ADODB.Stream")
        objStream.Type = 1 'adTypeBinary
        objStream.Open
        objStream.LoadFromFile(strFile)
        Response.Clear 
        'Response.ContentType = "image/jpeg"
        Response.Addheader "Content-Disposition", "attachment; filename=" & strFile
        Response.BinaryWrite objStream.Read
        objStream.Close
        Set objStream = Nothing

    End If
End Function    

Function SaveBinaryData(FileName, ByteArray)
  Const adTypeBinary = 1
  Const adSaveCreateOverWrite = 2

  'Create Stream object
  Dim BinaryStream
  Set BinaryStream = CreateObject("ADODB.Stream")

  'Specify stream type - we want To save binary data.
  BinaryStream.Type = adTypeBinary

  'Open the stream And write binary data To the object
  BinaryStream.Open
  BinaryStream.Write ByteArray

  'Save binary data To disk
  BinaryStream.SaveToFile FileName, adSaveCreateOverWrite
End Function
Function readBinary(path)
    Dim a, fso, file, i, ts
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set file = fso.getFile(path)
    If isNull(file) Then
        wscript.echo "File not found: " & path
        Exit Function
    End If
    Set ts = file.OpenAsTextStream()
    a = makeArray(file.size)
    i = 0
    While Not ts.atEndOfStream
       a(i) = ts.read(1)
       i = i + 1
    Wend
    ts.close
    readBinary = Join(a,"")
 End Function


 Sub writeBinary(bstr, path)
     Dim fso, ts
     Set fso = CreateObject("Scripting.FileSystemObject")
     On Error Resume Next
     Set ts = fso.createTextFile(path)
     If Err.number <> 0 Then
        wscript.echo Err.message
        Exit Sub
     End If
     On Error GoTo 0
     ts.Write(bstr)
     ts.Close
 End Sub

Function makeArray(n)
    Dim s
    s = Space(n)
    makeArray = Split(s," ")
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多