procedure TMainForm.Upload;
var
MutPartForm: TIdMultiPartFormDataStream;
i: Integer;
FileItem: TFileItem;
IdHTTP: TIdHTTP;
sl: TStringList;
begin
sl := TStringList.Create;
for i := 0 to FFiles.Count - 1 do
begin
MutPartForm := TIdMultiPartFormDataStream.Create;
IdHTTP := TIdHTTP.Create(nil);
try
try
IdHTTP.HandleRedirects := True;
FileItem := TFileItem(FFiles[i]);
MutPartForm.AddFile('userfile', FileItem.FilePath, 'text/plain');
IdHTTP.Post(FPostURL, MutPartForm);
sl.Add('上传文件成功:' + FileItem.FilePath);
except
on E: Exception do
begin
sl.Add('上传文件失败:' + FileItem.FilePath + ' Message:' + E.Message);
end;
end;
finally
MutPartForm.Free;
IdHTTP.Free;
end;
end;
sl.SaveToFile(ExtractFilePath(Application.ExeName) + 'Post.log');
sl.Free;
end;
var
MutPartForm: TIdMultiPartFormDataStream;
i: Integer;
FileItem: TFileItem;
IdHTTP: TIdHTTP;
sl: TStringList;
begin
sl := TStringList.Create;
for i := 0 to FFiles.Count - 1 do
begin
MutPartForm := TIdMultiPartFormDataStream.Create;
IdHTTP := TIdHTTP.Create(nil);
try
try
IdHTTP.HandleRedirects := True;
FileItem := TFileItem(FFiles[i]);
MutPartForm.AddFile('userfile', FileItem.FilePath, 'text/plain');
IdHTTP.Post(FPostURL, MutPartForm);
sl.Add('上传文件成功:' + FileItem.FilePath);
except
on E: Exception do
begin
sl.Add('上传文件失败:' + FileItem.FilePath + ' Message:' + E.Message);
end;
end;
finally
MutPartForm.Free;
IdHTTP.Free;
end;
end;
sl.SaveToFile(ExtractFilePath(Application.ExeName) + 'Post.log');
sl.Free;
end;
这段代码展示了如何使用 Indy 的 TIdHTTP 组件通过 HTTP 协议上传文件。它创建了一个 TIdMultiPartFormDataStream 对象来添加文件,然后调用 Post 方法将文件发送到指定 URL。如果在上传过程中发生异常,错误信息会被记录下来。
5850

被折叠的 条评论
为什么被折叠?



