iT邦幫忙

0

請問ASP CLASSIC如何做到後端上傳檔案

asp
  • 分享至 

  • xImage

各位先進日安,請教ASP CLASSIC如何利用serverXMLHTTP將json格式的資料,以檔案方式上傳到另外一台SERVER
如下圖
https://i.stack.imgur.com/RnOg9.png

目前我的語法如下:

Dim oHttp                    
Dim strResult
Set oHttp = CreateObject("MSXML2.serverXMLHTTP.6.0")
oHttp.setOption(2) = 13056
oHttp.open "POST", url, false
oHttp.setRequestHeader "SecretKey", skey
oHttp.setRequestHeader "AGENCY_ID", aid
oHttp.setRequestHeader "Content-Type", "application/json"
oHttp.send jsondata
oStr=oHttp.responseText
response.write oStr
看更多先前的討論...收起先前的討論...
Homura iT邦高手 1 級 ‧ 2021-09-03 16:32:07 檢舉
你是想用VB Script做嗎?
不然這個應該都是用js在做
跟你用asp沒關係
lungzen iT邦新手 4 級 ‧ 2021-09-03 16:39:19 檢舉
您好,這個如果用JS做,會有跨網域禁止問題,所以只能用後端做,謝謝您
天黑 iT邦研究生 5 級 ‧ 2021-09-03 16:46:40 檢舉
也許這是你需要
https://blog.xuite.net/avatar56/balmywind/335945795-ASP+-+VBScript+-+POST%E8%B3%87%E6%96%99+with+XMLHTTP
lungzen iT邦新手 4 級 ‧ 2021-09-03 16:50:09 檢舉
Hi horace_work:
這個有試過了,之前我也是這麼做都可以,但是這次卻不行
Homura iT邦高手 1 級 ‧ 2021-09-03 16:51:24 檢舉
話說你不是跟對方要串接服務
怎麼對方沒有允許跨網域上傳呢?
lungzen iT邦新手 4 級 ‧ 2021-09-03 16:53:07 檢舉
Hi Homura:
謝謝回覆,對方是政府單位,不給允許
天黑 iT邦研究生 5 級 ‧ 2021-09-03 16:53:52 檢舉
那要不要試試改成打json 不要用form data上傳 , 因為不確定你接收的方式是什麼 或是你要提供比較完整接收失敗的錯誤訊息
附上 json的參考: https://stackoverflow.com/questions/8144368/can-a-classic-asp-page-using-xmlhttp-make-a-json-request
Homura iT邦高手 1 級 ‧ 2021-09-03 16:54:08 檢舉
horace_work提供的做法你要改成formData唷....
範例是傳json
你貼的是用fromData的方式
lungzen iT邦新手 4 級 ‧ 2021-09-03 16:57:14 檢舉
header改成form-data的方式我試過,也是不行,關鍵在於postman的最後一欄
Homura iT邦高手 1 級 ‧ 2021-09-03 16:59:51 檢舉
https://stackoverflow.com/questions/37436516/file-updload-in-post-form-in-vbs
這個轉base64的試試
lungzen iT邦新手 4 級 ‧ 2021-09-03 17:01:51 檢舉
Hi Homura:
謝謝回覆,但是感覺他們沒辦法接收base64
天黑 iT邦研究生 5 級 ‧ 2021-09-03 17:19:03 檢舉
所以是卡在不知道怎樣把json file 弄到form data裡面嗎?
應該可以參考這邊 https://stackoverflow.com/questions/36217009/classic-asp-file-upload-with-http-post-request
lungzen iT邦新手 4 級 ‧ 2021-09-03 18:20:35 檢舉
Hi horace_work:
謝謝回覆,我研究看看
lungzen iT邦新手 4 級 ‧ 2021-09-03 19:35:43 檢舉
Hi horace_work:
測試後不行,即使我將json資料先暫存在暫存資料夾再上傳一樣不行
暫存檔案利用postman是可以的

Dim objFSO, objTextFile
Dim sRead, sReadLine, sReadAll
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.CreateTextFile(file_name,ForAppending,True)

objTextFile.WriteLine(jsondata)

objTextFile.Close

Dim BINARYPOST
Set stream = Server.CreateObject("ADODB.Stream")
stream.Type = adTypeBinary
stream.Mode = adModeReadWrite
stream.Open
stream.Write StringToBinary("" & boundary & vbCrLf & _
"Content-Disposition: form-data; name=""file""; file=""" & file_name & """" & _
"Content-Type: json" & vbCrLf & vbCrLf)
stream.Write ReadBinaryFile(file_name)
stream.Write StringToBinary(vbCrLf & boundary & "--")
stream.Position = 0
BINARYPOST= stream.read
stream.Close

' Send HTTP POST
set xmlhttp = CreateObject("Msxml2.ServerXMLHTTP.6.0")
xmlhttp.open "POST", url, false
xmlhttp.setRequestHeader "SecretKey", skey
xmlhttp.setRequestHeader "AGENCY_ID", aid
xmlhttp.setRequestHeader "Content-Type", "application/json"
'xmlHttp.setRequestHeader "content-type", "multipart/form-data; boundary=" & boundary
xmlhttp.setRequestHeader "Content-Disposition","form-data; name=jsonfile; filename=" & file_name
xmlhttp.send BINARYPOST
lungzen iT邦新手 4 級 ‧ 2021-09-04 11:18:54 檢舉
提供postman的CURL,請各位幫幫忙
curl --location --request POST 'https://nqqqq.gov.tw/nqata' \
--form 'SecretKey="111111111111111111111111111111"' \
--form 'AGENCY_ID="2222222222"' \
--form 'jsonfile=@"/C:/Users/Ken/Downloads/202109021140.json"'
lungzen iT邦新手 4 級 ‧ 2021-09-05 14:03:13 檢舉
感恩大家,目前參考以下資料,已經可以遠端接收資料
https://gist.github.com/mdauphin/7c162832238aea0f9001bbd1bef75b86
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
BattleProgramer
iT邦新手 5 級 ‧ 2021-09-04 09:25:34
最佳解答

以我一個新人類,沒有接觸過ASP,但是我會ASP.NET 跟 HTML JS ,我會建議您另外架設檔案管理服務網站,提供上傳的服務,上傳完成後把連結丟給ASP就好。

lungzen iT邦新手 4 級 ‧ 2021-09-04 11:05:03 檢舉

謝謝建議^_^

lungzen iT邦新手 4 級 ‧ 2021-09-10 09:26:27 檢舉

到最後還是用這方法,設一個中繼站去post,感恩

0
dscwferp
iT邦高手 1 級 ‧ 2021-09-03 20:41:50

我參考這篇: https://www.796t.com/post/MjAyc3M=.html
asp 也可以用喔!
請參考!

看更多先前的回應...收起先前的回應...
lungzen iT邦新手 4 級 ‧ 2021-09-03 21:03:36 檢舉

謝謝解答,有點小複雜,我研究看看

不過,這key跟id的header要家再哪邊?感謝萬分

dscwferp iT邦高手 1 級 ‧ 2021-09-03 22:26:32 檢舉

參靠這篇: https://www.motobit.com/tips/detpg_sendfrmie/
將 key跟id 放到 url
一起post!

lungzen iT邦新手 4 級 ‧ 2021-09-04 12:12:57 檢舉

您好,請問InternetExplorer.Application無法使用怎麼辦?

dscwferp iT邦高手 1 級 ‧ 2021-09-04 16:35:51 檢舉

WIN 2019 & 10 都還有 InternetExplorer.Application 耶!
您的環境是?

lungzen iT邦新手 4 級 ‧ 2021-09-04 18:01:58 檢舉

您好,是server 2012

dscwferp iT邦高手 1 級 ‧ 2021-09-04 19:06:09 檢舉

在2012上用 admin開啟IE 連上您的ASP URL
比如 http://127.0.0.1/XXX.asp
測試看看!

lungzen iT邦新手 4 級 ‧ 2021-09-05 14:02:18 檢舉

Hi dscwferp:
我參考這個,目前試OK了,可以遠端接到檔案,感恩
https://gist.github.com/mdauphin/7c162832238aea0f9001bbd1bef75b86

我要發表回答

立即登入回答