csCommon.pas 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. unit csCommon;
  2. interface
  3. uses
  4. SOAPHTTPClient, IdMultipartFormData,
  5. IdHttp, Forms, Windows, csConfigInfo,
  6. ShellAPI, csProj;
  7. procedure InitHttpRioWSDL(AHttpRio: THTTPRIO);
  8. procedure Upload(AIdHttp: TIdHTTP; const AZipFile, AURL: string);
  9. procedure CreateDirIfNotExists(const ADir: string);
  10. function DeleteFiles(const AFilePath: string): Boolean;
  11. implementation
  12. uses SysUtils;
  13. procedure InitHttpRioWSDL(AHttpRio: THTTPRIO);
  14. begin
  15. AHttpRio.WSDLLocation := IntfConfig.WSDL;
  16. AHttpRio.Service := 'CostService';
  17. AHttpRio.Port := 'CostServiceHttpSoap11Endpoint';
  18. end;
  19. procedure Upload(AIdHttp: TIdHTTP; const AZipFile, AURL: string);
  20. procedure HintSucessOrFailed(const AText: string);
  21. var
  22. iPos: Integer;
  23. begin
  24. iPos := Pos(';', AText);
  25. if iPos > 0 then
  26. begin
  27. if Copy(AText, 1, iPos - 1) = 'true' then
  28. begin
  29. Application.MessageBox('文件上传成功! ', '提示', MB_OK + MB_ICONINFORMATION);
  30. csCurProj.ProjBatID := Copy(AText, iPos + 1, Length(AText) - iPos);
  31. end
  32. else
  33. begin
  34. Application.MessageBox('文件上传失败! ', '提示', MB_OK + MB_ICONWARNING);
  35. csCurProj.ProjBatID := '';
  36. end;
  37. end
  38. else
  39. begin
  40. if AText = 'true' then
  41. Application.MessageBox('文件上传成功! ', '提示', MB_OK + MB_ICONINFORMATION)
  42. else
  43. Application.MessageBox('文件上传失败! ', '提示', MB_OK + MB_ICONWARNING);
  44. end;
  45. end;
  46. procedure AddFormFields(
  47. AIdDataStream: TIdMultiPartFormDataStream; AZipFile: string);
  48. begin
  49. AIdDataStream.AddFormField('files', AZipFile);
  50. AIdDataStream.AddFormField('Submit', 'Submit');
  51. //AIdDataStream.AddFile('files', AZipFile, 'multipart/form-data');
  52. AIdDataStream.AddFile('files', AZipFile, 'text/plain');
  53. end;
  54. procedure UpLoadZipFile(
  55. AIdDataStream: TIdMultiPartFormDataStream; AURL: string);
  56. begin
  57. try
  58. AIdDataStream.Position := 0;
  59. HintSucessOrFailed(AIdHTTP.Post(AURL, AIdDataStream));
  60. except
  61. Application.MessageBox('远程服务器连接失败!', '提示', MB_OK + MB_ICONWARNING);
  62. end;
  63. end;
  64. var
  65. IdDataStream: TIdMultiPartFormDataStream;
  66. begin
  67. IdDataStream := TIdMultiPartFormDataStream.Create;
  68. try
  69. AddFormFields(IdDataStream, AZipFile);
  70. UpLoadZipFile(IdDataStream, AURL);
  71. finally
  72. IdDataStream.Free;
  73. end;
  74. end;
  75. procedure CreateDirIfNotExists(const ADir: string);
  76. begin
  77. if not DirectoryExists(ADir) then
  78. ForceDirectories(ADir);
  79. end;
  80. function DeleteFiles(const AFilePath: string): Boolean;
  81. var
  82. Fo: TSHFileOpStruct;
  83. begin
  84. FillChar(Fo, SizeOf(fo), 0);
  85. with Fo do
  86. begin
  87. Wnd := 0;
  88. wFunc := FO_DELETE;
  89. pFrom := PChar(AFilePath + #0);
  90. pTo := #0#0;
  91. fFlags := FOF_NOCONFIRMATION + FOF_SILENT;
  92. end;
  93. Result := (SHFileOperation(Fo) = 0); // 成功返回0
  94. end;
  95. end.