频道分类

delphi Upload 上传文件

作者:admin 来源: 日期:2020/4/9 13:52:21 人气: 标签:

 
uses System.Net.Mime,System.Net.HttpClient;

function Upload(const aFilePath: string): boolean;
var
  vData: TMultipartFormData; // uses System.Net.Mime
  vHTTP: THTTPClient; // uses System.Net.HttpClient
  vCRC: cardinal;
  vURL: string;
  vResp: TStringStream;
begin
  vURL := 'PHP url';
  vResp := TStringStream.Create('');
  vData := TMultipartFormData.Create();
  vHTTP := THTTPClient.Create;
  try
    try
      vData.AddField('version', MyVerField.ToString);
      vData.AddField('crc', MyCRC.ToString);

      vData.AddFile('db_file', aFilePath);
      Result := vHTTP.Post(vURL, vData, vResp).StatusCode = 200;

      if Result then 
        Result := vResp.DataString.ContentAsString().Contains('"result":true');          

    except
      Result := false;
    end;
  finally
    vHTTP.Free;
    vData.Free;
    vResp.Free; 
  end;
end;