Delphi专题 ·

Delphi使用Indy组件http上传文件

功能

该示例使用Indy组件演示如何使用http上传文件
Delphi使用Indy组件http上传文件 Delphi专题 第1张

主要代码

procedure TformMain.btnUploadClick(Sender: TObject);
var
  formData : TIdMultiPartFormDataStream;
  i: integer;
begin
  if (ListBox_Filelist.Items.Count = 0) then exit;
  
  formData := TIdMultiPartFormDataStream.Create;

  try
    for i := 0 to ListBox_FileList.Items.Count -1 do
    begin
      formData.AddFile('File'+IntToStr(i), ListBox_FileList.Items[i], 'plain/text');
    end;

    IdHTTP1.Request.Referer := Edit_URLAddress.Text;

    try
      memo_log.Lines.Text     := idHTTP1.Post(Edit_URLAddress.Text, formData);
    except
      on e: exception do
      begin
        memo_log.Lines.Text   := e.Message;
      end;
    end;
  finally
    formData.Free;
  end;
end;

procedure TformMain.btn_ClearMessagesClick(Sender: TObject);
begin
//删除所有行信息
  with memo_log.Lines do
  begin
    BeginUpdate;
    Clear;
    EndUpdate;
  end;
end;

procedure TformMain.btnRemoveFileClick(Sender: TObject);
begin
  // cn: 从filelist清除所选行的项目
  // en: To remove the selected itens from filelist
  if ListBox_Filelist.SelCount > 0 then
  ListBox_Filelist.DeleteSelected
  else
  showmessage('请先选定要删除的项目 / Please select the item(s) to delete');
end;

procedure TformMain.btnAddFileClick(Sender: TObject);
begin
  // cn: 从对话框中添加新文件到filelist
  // en: To add a new file at filelist from dialog box
  if (OpenDialog1.Execute) then ListBox_Filelist.Items.Add(OpenDialog1.FileName);
end;

procedure TformMain.Exit1Click(Sender: TObject);
begin
  Application.Terminate;
end;

相关下载

点击下载

参与评论