#2
yiyanxiyin2024-08-19 10:49
可以试一下WinHttp.WinHttpRequest.5.1这个控件, 参考代码:
LOCAL loHttpRequest AS "WinHttp.WinHttpRequest.5.1" LOCAL lcUrl AS STRING, lcBoundary AS STRING LOCAL lcBody AS STRING, lcFileContent AS STRING LOCAL lcFileName AS STRING, lcFilePath AS STRING LOCAL lcFormDataField AS STRING, lcFormDataValue AS STRING * 初始化变量 lcUrl = "http:// lcBoundary = "VFPBoundary" + TRANSFORM(DATETIME(), 1) lcFilePath = "C:\path\to\your\image.jpg" lcFileName = JUSTFNAME(lcFilePath) lcFormDataField = "anotherField" lcFormDataValue = "SomeValue" * 读取文件内容 lcFileContent = FILETOSTR(lcFilePath) * 构建 multipart/form-data 请求体 (重点修改代码: 这里就是图片) lcBody = "--" + lcBoundary + CRLF lcBody = lcBody + "Content-Disposition: form-data; name=\"" + "file"; + "\"; filename=\"" + lcFileName + "\"" + CRLF lcBody = lcBody + "Content-Type: image/jpeg" + CRLF + CRLF lcBody = lcBody + lcFileContent + CRLF lcBody = lcBody + "--" + lcBoundary + CRLF * 重点修改代码: 这里就是表单域,对应你的json lcBody = lcBody + "Content-Disposition: form-data; name=\"" + lcFormDataField + "\"" + CRLF + CRLF lcBody = lcBody + lcFormDataValue + CRLF lcBody = lcBody + "--" + lcBoundary + "--" * 创建 WinHttpRequest 对象 CREATE OBJECT loHttpRequest AS "WinHttp.WinHttpRequest.5.1" * 打开连接 loHttpRequest.Open("POST", lcUrl, FALSE) * 设置请求头 loHttpRequest.SetRequestHeader("Content-Type", "multipart/form-data; boundary=" + lcBoundary) * 发送请求 loHttpRequest.Send(lcBody) * 等待响应并处理 IF loHttpRequest.Status = 200 THEN ? "Response Text: ", loHttpRequest.ResponseText ELSE ? "Error: ", loHttpRequest.Status & " " & loHttpRequest.StatusText ENDIF * 清理 RELEASE loHttpRequest |
这是一个上传图像到远程服务器,报文的头部还比较清楚,但是报文内容的结构还没弄清楚, 因为这不是单纯传一个图像,如果单纯只传一张图像,
我认为可能好办,但是这里还要首先附加上传一个 json,这个json 的内容,是为了说明图像是什么人的,有身份证号sfzh,也有检查日期 examDate,有体检类型 type 5(心电图是5),现在是,如何将一个图像文件 mn.jpg,配合json,构成报文内容一并上传。
敬请高手们过目一下,再次感谢!!!
string url = report_addr +$"thirdApi/saveHealthExamPile":
NameValueCollection nvc = new NameValueCollection ();
nve.Add("param,“{\“idNo\ :\ + sfzh + “\’,\"examDate\":\"" + tjb. Jdrq. Value. ToString("yyyy-M-dd) + "\, \"type\":\"5\}"):
var task = uploadGXTJ(lt.nvc,ms. ToArray(),url,partner_license):
async
Task<qmResult>
uploadGXTJ
LoginToken
lt.
NameValueCollection formData,
byte[]
file.
string url,
string partner_license)
try
using(HttpClient webClient = new HttpClient())
// 添加表单数据
//webClient.Headers.Add("Content-Type","multipart/form-data");
webClient. DefaultRequestHeaders.AcceptEncoding. Add(new StringWithQualityHeaderValue("utf-8"));
webClient. DefaultRequestHeaders.Add("access_token", lt.access_token);
webClient. DefaultRequestHeaders.Add("partner-license", partner_license);
using (var mformData = new MultipartFormDataContent ()
var fileContent = new StreamContent(new MemoryStream(file));
fileContent.Headers.ContentType = MediaTypeHeaderValue. Parse("multipart/form-data");
mformData.Add(fileContent,“file", Path.GetFileName("mn.jpg"));
foreach (var key in formData.AllKeys)
var keyValueContent = new StringContent(formData[key]):
mformData.Add (keyValueContent, key): // 替换为你的表单键名
[此贴子已经被作者于2024-8-19 08:39编辑过]