1

I have an attachment field in my accdb database file, i'm trying to read it to extract the attachments but it keep return empty values

recording to this post Using Attachment field with Classic ASP there is no way to do it with adodb, is true? and if yes, what other ways i have to do that ?

this is the code that i'm running:

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
Community
  • 1
  • 1
Rami Khawaly
  • 126
  • 3
  • 14
  • Some update, after a long day of trying, This query brings the attachments, but still, i don't know how to serialize the data SQL = "select Attachments.FileName as fname, Attachments.FileData as data, Attachments.FileType as FileType from [Knowledge Base] where id="&qid Any ideas ?? – Rami Khawaly May 14 '17 at 15:27

1 Answers1

0

For now, i did some workaround that helped me, it's not efficient but does the work.

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
Rami Khawaly
  • 126
  • 3
  • 14