iT邦幫忙

0

請教各位大大 : 如何利用 classic ASP 把base64編碼轉存圖檔?

  • 分享至 

  • xImage

您好

  1. 如何利用 classic ASP 把base64編碼轉存圖檔? (網站是古老的ASP)
    因為參考網路上的程式 , 發生錯誤.
  2. 利用下方程式 , 當imageData 長度操過約4800後就會發生錯誤 ,
    卡在 xml.documentElement.dataType = "bin.base64" 這行
      msxml3.dll 錯誤 '80004005'
      剖析 'data:image/png;base64,iVB ...' 為 bin.base64 資料型別時發生錯誤。
* 環境 : windows 2012 
* Canvas2Image.html > ajax 編碼base64 > upload.asp
* Canvas2Image.html 取圖寬高約750x1800 , 檔案大小約 120KB

請問如何解決這問題?

先謝謝大家!

upload.asp

<%@Language="VBScript" CodePage=65001 %>
<%
'https://blog.patw.me/archives/456/asp-base64-string-transcribed-into-jpg/

'On Error Resume Next
'response.flush
imageData= "data:image/png;base64,...."

imageData=replace(imageData,"data:image/jpeg;base64,","") 
Function getTimeToString()
	Dim MyNewRandomNum
	Randomize
	MyNewRandomNum = Int(Rnd * 100)+1
	d = Now()
	getTimeToString = Year(d) & "_" & Month(d) & "_" & Day(d) & "_" & Hour(d) & "_" & Minute(d) & "_" & Second(d) & "_" & MyNewRandomNum
End Function

Dim filename
filename = getTimeToString() & ".jpg"

Dim xml : Set xml=Server.CreateObject("MSXML2.DOMDocument")
Dim stm : Set stm=Server.CreateObject("ADODB.Stream")
xml.resolveExternals=False
xml.loadXML "<?xml version='1.0'?><data><![CDATA[" & imageData & "]]></data>"
xml.documentElement.setAttribute "xmlns:dt","urn:schemas-microsoft-com:datatypes"
xml.documentElement.dataType = "bin.base64"
stm.Type=1 'adTypeBinary
stm.Open
stm.Write xml.documentElement.nodeTypedValue
stm.SaveToFile  filename
stm.Close
Set xml=Nothing
Set stm=Nothing

IF err.number<>0 THEN
	Response.Write "Result=Failed&msg=發生錯誤"
ELSE
	Response.Write "Result=" & filename '輸出檔名
END IF
%>

2020-10-24
問題解決: 改寫ajax

   let url = "upload.asp";
    let term =img_data;
    let posting = $.post( url, { picstr: term } );
      posting.done(function( data ) {
        alert(data);
      });
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
dscwferp
iT邦高手 1 級 ‧ 2020-10-23 17:30:19
Function Base64ToBin(FileName,ToFileName)
	Const foForReading          = 1 ' Open base 64 code file for reading
	Const foAsASCII             = 0 ' Open base 64 code file as ASCII file
	Const adSaveCreateOverWrite = 2 ' Mode for ADODB.Stream
	Const adTypeBinary          = 1 ' Binary file is encoded

	' Variables for reading base64 code from file
	Dim objFSO
	Dim objFileIn
	Dim objStreamIn

	' Variables for decoding
	Dim objXML
	Dim objDocElem

	' Variable for write binary picture
	Dim objStream

	' Open data stream from base64 code filr
	'Set objFSO = CreateObject("Scripting.FileSystemObject")
	'Set objFileIn   = objFSO.GetFile(FileName)
	'Set objStreamIn = objFileIn.OpenAsTextStream(foForReading, foAsASCII)

	' Create XML Document object and root node
	' that will contain the data
	Set objXML = CreateObject("MSXml2.DOMDocument")
	Set objDocElem = objXML.createElement("Base64Data")
	objDocElem.DataType = "bin.base64"

	' Set text value
	'objDocElem.text = objStreamIn.ReadAll()
	objDocElem.text =FileName
	' Open data stream to picture file
	Set objStream = CreateObject("ADODB.Stream")
	objStream.Type = adTypeBinary
	objStream.Open()

	' Get binary value and write to file
	objStream.Write objDocElem.NodeTypedValue
	objStream.SaveToFile ToFileName, adSaveCreateOverWrite

	' Clean all
	Set objFSO = Nothing
	Set objFileIn = Nothing
	Set objStreamIn = Nothing
	Set objXML = Nothing
	Set objDocElem = Nothing
	Set objStream = Nothing
End Function

網路上找到的!
用 MSXml2.DOMDocument 元件!
使用方法:
Base64ToBin photoBase64,Server.MapPath("photo.jpg")
來源參考: https://gist.github.com/matheuseduardo/6d205904ec66cf4332c91cb539729ce4

webtest iT邦新手 5 級 ‧ 2020-10-23 18:02:13 檢舉

謝謝!

我要發表回答

立即登入回答