簡體   English   中英

將本地文件上傳到服務器時出現“非多路徑請求”錯誤

[英]“not a multipath request” error when uploading local file to server

OPTION EXPLICIT

DIM myFile, myDate, myName, xml

myName = "shipment"
myDate = "20150708"

'The file we are uploading

myFile = "C:\Users\TestUser\Desktop\myFile.txt"

Set xml = CreateObject("Microsoft.XMLHTTP")
xml.Open "POST", "http://localhost:8080//myProject/myClass/myMethod", myName, myDate, myFile, False
xml.Send
Set xml = Nothing

對於上面的VBScript,我得到了錯誤

參數數量錯誤或屬性分配無效。

我通過將上面的代碼更改為下面的代碼修復了上面的錯誤

...
...
Set xml = CreateObject("Microsoft.XMLHTTP")

xml.Open "POST", "http://localhost:8080//myProject/myClass/myMethod" , False  
xml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xml.Send "sName=shipment&sDate=20150708&sFile=C:\Users\TestUser\Desktop\myFile.txt"

Set xml = Nothing

現在我在服務器端收到以下錯誤:

錯誤:當前請求不是多部分請求

FileUpload.java (Java中的服務器端代碼):

...
...
@RequestMapping(value = "/myMethod", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody String processData(
        @RequestParam("myName") String fileName,
        @RequestParam("myDate") String fileDate,
        @RequestParam("myFile") MultipartFile file,
        HttpServletResponse response) throws Exception {
 ...
 ...

那是因為您實際上並沒有發送文件。 您只是將文件名作為字符串發送。 您不能只是將文件名分配給form / post變量並期望VBScript上載它。 您需要打開文件並將字節流作為HTTP請求的一部分。 例如:

Const adTypeBinary = 1
Const adModeReadWrite = 3

' Load the file into an ADO Stream object... '
Dim objStream
Set objStream = CreateObject("ADODB.Stream")
objStream.Mode = adModeReadWrite
objStream.Type = adTypeBinary
objStream.Open
objStream.LoadFromFile "C:\Users\TestUser\Desktop\myFile.txt"

' Create an XML/HTTP request... '
Dim objHttp
Set objHttp = CreateObject("MSXML2.XMLHTTP")
objHttp.Open "POST", "http://localhost:8080//myProject/myClass/myMethod", False
objHttp.SetRequestHeader "Content-Length", xmlStream.Size
objHttp.SetRequestHeader "Content-Type", "multipart/form-data"

' Now read the entire stream and send it in the request body... '
objHttp.Send objStream.Read(objStream.Size)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM