procedure TDM.HTTPDownloadFile(FFilePath, FDownFilePath: WideString);
var
HttpClient: TIdHTTP;
MutPartForm: TIdMultiPartFormDataStream;
MyStream: TMemoryStream;
jsonObject: ISuperObject;
sResponseData: string;
begin
try
try
//创建资源
HttpClient := TIdHttp.Create(nil);
MutPartForm := TIdMultiPartFormDataStream.Create;
MyStream := TMemoryStream.Create;
//设置IDHTTP请求头信息
HttpClient.Request.Accept := '*/*';
HttpClient.Request.AcceptLanguage := 'zh-cn';
HttpClient.Request.ContentType := 'application/json;charset=UTF-8';
HttpClient.ProtocolVersion := pv1_1;
//服务器文件路径
MutPartForm.AddFormField('path', UTF8Encode(FDownFilePath));
//发送POST请求
HttpClient.Post(g_sHttpDownUrl, MutPartForm, MyStream);
SetString(sResponseData, PAnsiChar(MyStream.Memory), MyStream.Size);
sResponseData := Utf8ToAnsi(sResponseData);
if Pos('404', sResponseData) > 0 then
begin
MyCommon.DoTip(False);
jsonObject := SO(sResponseData);
MyCommon.WriteLog('下载' + FDownFilePath + '文件失败,原因为: [404错误]' + jsonObject['message'].AsString);
Exit;
end;
MyStream.SaveToFile(FFilePath);
except
on e: Exception do
begin
MyCommon.WriteLog('下载' + FDownFilePath + '文件失败,原因为' + e.Message);
end;
end;
finally
MyStream.Free;
MutPartForm.Free;
HttpClient.Disconnect;
HttpClient.Free;
end;
end;