2017-05-14 16 views
1

我在ACCDB數據庫文件有一個附件欄, 我試圖讀取它以提取附件,但保持它的返回值爲空的讀取附件欄從經典ASP3 ACCDB/VBScript中

記錄這一職務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 
+0

一些更新,試圖一整天后, 該查詢帶來的附件,不過,我不知道如何序列數據 SQL =「選擇Attachments.FileName爲FNAME,附件。 FileData as data,Attachments.FileType as FileType from [知識庫]其中id =「&qid 任何想法?? –

回答

0

現在,我做了一些解決方法,幫助我,這不是有效的,但做的工作。

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