iT邦幫忙

0

Classic ASP image圖檔轉換成Base64

  • 分享至 

  • xImage

大大們好 請問要怎麼使用VBscript 將image圖檔轉換成base64 然後寫入.txt file呢?
下面是我目前所做的, 但好像程式沒正常執行也沒寫入到.txt file

Function convertImageToBase64(filePath)
  'response.write(Server.MapPath("\") & "<br>")
  dim fs,f
  set fs=Server.CreateObject("Scripting.FileSystemObject")
  set f=fs.CreateTextFile(Server.MapPath("\") &"\Upload\5566.txt",true)
  
  Dim inputStream
  Set inputStream = CreateObject("ADODB.Stream")
  inputStream.Open
  inputStream.Type = 1  ' adTypeBinary
  inputStream.LoadFromFile filePath
  Dim bytes: bytes = inputStream.Read
  Dim dom: Set dom = CreateObject("Microsoft.XMLDOM")
  Dim elem: Set elem = dom.createElement("tmp")
  elem.dataType = "bin.base64"
  elem.nodeTypedValue = bytes
  convertImageToBase64 = "data:image/png;base64," & Replace(elem.text, vbLf, "")

  f.write(convertImageToBase64)
  set f=nothing
  set fs=nothing
  f.close
  
  End Function

convertImageToBase64(RootPath&"/upload/"&Rs("FileName"))

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
純真的人
iT邦大師 1 級 ‧ 2021-04-20 23:50:17
最佳解答

你不是會寫.Net嗎?
這種傳統ASP就別糾結了~
直接傳給.Net去轉換就好了~還比較簡單@@a

因為以前的ASP網站維護~我都混搭.Net網頁使用...


我執行了一下~有些位置跟寫入權限要開~

ASP錯誤訊息~我是從Wondow事件簿查到的..
https://ithelp.ithome.com.tw/upload/images/20210421/20061369vMT37tWUJS.png
https://ithelp.ithome.com.tw/upload/images/20210421/20061369tQCJaek36L.png

寫入權限要開~
https://ithelp.ithome.com.tw/upload/images/20210421/20061369xnackSjOfT.png

'位置有錯
set f=nothing
set fs=nothing
f.close

我測試內容如下~

<% 
Function convertImageToBase64(filePath)

    dim fs,f
    set fs = Server.CreateObject("Scripting.FileSystemObject")
    set f = fs.CreateTextFile(Server.MapPath("./") & "\Test.txt",true)
  
    Dim inputStream
    Set inputStream = CreateObject("ADODB.Stream")
    inputStream.Open
    inputStream.Type = 1
    inputStream.LoadFromFile filePath
    Dim bytes: bytes = inputStream.Read
    Dim dom: Set dom = CreateObject("Microsoft.XMLDOM")
    Dim elem: Set elem = dom.createElement("tmp")
    elem.dataType = "bin.base64"
    elem.nodeTypedValue = bytes
    convertImageToBase64 = "data:image/png;base64," & Replace(elem.text, vbLf, "")

    f.write(convertImageToBase64)
    f.close
    set f=nothing
    set fs=nothing


End Function

GetPath = Server.MapPath("./") & "\Test.jpg"

convertImageToBase64(GetPath)

Response.write Now

%>

https://ithelp.ithome.com.tw/upload/images/20210421/20061369rXc0EKiDfm.png

寫入檔案
https://ithelp.ithome.com.tw/upload/images/20210421/20061369dJBuUCg2Xa.png

內容更新了~

小火車 iT邦新手 4 級 ‧ 2021-04-21 17:50:44 檢舉

感恩啊 大大 後來我也發現權限寫入問題 真的非常感謝

我要發表回答

立即登入回答