问题
· 八月 19

ensemble中如何调用服务上传excel文件,请求头类型multipart/form-data;出现一堆调试乱码,是文件类型设置的问题还是哪里的问题?

1.我在java中发布了一个服务提供给调用,postman中可以请求解析出数据

请求信息如下:

POST /uploadPersonExcel HTTP/1.1
Host: localhost:8017
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
cache-control: no-cache
Postman-Token: 1c23edfe-1e83-44c6-8f91-e06bccfd4af3

Content-Disposition: form-data; name="file"; filename="C:\Users\elite\Desktop\personinfo.xlsx

------WebKitFormBoundary7MA4YWxkTrZu0gW--

 

2.Ensemble中请求代码如下:

ClassMethod PostFile()
{
	    Set excelpath = "C:\Users\elite\Desktop\personinfo.xlsx"
	    s filename="personinfo.xlsx"
	    s fullPathName=excelpath
        Set HttpRequest = ##class(%Net.HttpRequest).%New() 
        set HttpRequest.Server="127.0.0.1"
	    set HttpRequest.Port="8017"
	    //set HttpRequest.AcceptGzip=0
        //d HttpRequest.SetHeader("Accept", "'*/*'")
        //d HttpRequest.SetHeader("Access-Control-Allow-Origin","*")
        s stream=##class(%Stream.FileBinary).%New()
		s sc=stream.LinkToFile(excelpath) 
		//多个文件    
		s RootMIMEPart = ##class(%Net.MIMEPart).%New()
		//单个文件
		s BinaryMIMEPart = ##class(%Net.MIMEPart).%New()
		s BinaryMIMEPart.Body = stream
		;d BinaryMIMEPart.SetHeader("Content-Type", "application/octet-stream")
		d BinaryMIMEPart.SetHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
		d BinaryMIMEPart.SetHeader("Content-Disposition","form-data; name="_$CHAR(34)_"file"_$CHAR(34)_"; filename="_$CHAR(34)_filename_$CHAR(34)_"")
		S status = RootMIMEPart.Parts.Insert(BinaryMIMEPart)
		s writer = ##class(%Net.MIMEWriter).%New()
		d writer.OutputToStream(HttpRequest.EntityBody)
		d writer.WriteMIMEBody(RootMIMEPart)
		d HttpRequest.SetHeader("Content-Type", "multipart/form-data; boundary="_RootMIMEPart.Boundary)
		d HttpRequest.SetHeader("Content-Disposition","form-data; name="_$CHAR(34)_"file"_$CHAR(34)_"; filename="_$CHAR(34)_excelpath_$CHAR(34)_"")    
		d HttpRequest.InsertFormData("file", BinaryMIMEPart)
		s tSCx = HttpRequest.Post("/uploadPersonExcel",1, 1)   
        w:tSCx'=1 $System.Status.GetErrorText(tSCx)
		If '$IsObject( HttpRequest.HttpResponse) {
		      s err = "Response Error"
		      //$$$TRACE(err)
		} 
		Set Myresponse = HttpRequest.HttpResponse
		If $IsObject(Myresponse) {
		set data1 = ""
		set stream1 = Myresponse.Data
		While 'stream1.AtEnd { 
			set data1 = data1 _ stream1.ReadLine(,.sc,.eol)
			If $$$ISERR(sc) { 
			   set data1 = data1 _ "ERROR" Quit
			} 
		}
		w "返回结果:"_data1,!
		}
}

测试模式下的消息如下:

不知道问题出在哪里?

产品版本: Ensemble 2016.1
$ZV: Cache for Windows (x86-64) 2016.2.3 (Build 907_11_20446U) Thu Nov 12 2020 16:56:45 EST
讨论 (2)2
登录或注册以继续