大大們好 請問要怎麼使用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"))
你不是會寫.Net嗎?
這種傳統ASP就別糾結了~
直接傳給.Net去轉換就好了~還比較簡單@@a
因為以前的ASP網站維護~我都混搭.Net網頁使用...
我執行了一下~有些位置跟寫入權限要開~
ASP錯誤訊息~我是從Wondow事件簿查到的..
寫入權限要開~
'位置有錯
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
%>
寫入檔案