idhttp文件的上传和下载
2024-09-06 16:08:20
unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP, StdCtrls; type
TForm1 = class(TForm)
btn1: TButton;
IdHTTP1: TIdHTTP;
procedure btn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} procedure TForm1.btn1Click(Sender: TObject);
var
iLen:integer;
sFN,sPath:string;
MS,MS2:TMemoryStream;
begin
MS:=TMemoryStream.Create;
try
sPath:=ExtractFilePath(application.ExeName)+'a.bmp';
sFN:=ExtractFileName(sPath);
iLen:=length(sFN); MS.WriteBuffer(iLen,sizeof(iLen));
MS.WriteBuffer(sFN[],iLen); MS2:=TMemoryStream.Create;
MS2.LoadFromFile(sPath); iLen:=MS2.Size; MS.WriteBuffer(iLen,sizeof(iLen)); MS.CopyFrom(MS2,iLen);
MS2.Free;
IDHTTP1.post('http://127.0.0.1:2111',MS,nil);
finally
MS.Free;
end;
end; procedure TForm1.FormCreate(Sender: TObject);
begin
// IdHTTP1.Host:= '127.0.0.1';
// IdHTTP1.Port:= 2111;
end; end. unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPServer, IdCustomHTTPServer,
IdHTTPServer; type
TForm1 = class(TForm)
IdHTTPServer1: TIdHTTPServer;
procedure IdHTTPServer1CommandGet(AThread: TIdPeerThread;
ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} procedure TForm1.IdHTTPServer1CommandGet(AThread: TIdPeerThread;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
iLen:Integer;
sData,sFN:string;
MS:TMemoryStream;
pData:PChar;
begin
sData:=ARequestInfo.UnparsedParams;
if sData<>'' then
begin
{
自己定义一个简单的消息格式,数据长度1,数据1,数据长度2,数据2 |文件名长度|文件名|文件内容长度|文件内容|
|5|a.bmp|17788|0x10101010101001010100101...| } MS:=TMemoryStream.Create;
try
//1读取文件名
MS.WriteBuffer(sData[],length(sData));
MS.Position:=;
MS.ReadBuffer(iLen,sizeof(iLen)); //读文件名长度
SetLength(sFN,iLen);
MS.ReadBuffer(sFN[],iLen); //读文件名
//2读取文件内容
MS.ReadBuffer(iLen,sizeof(iLen));//读文件内容长度
GetMem(pData,iLen);
MS.ReadBuffer(pData^,iLen);//读文件内容 pData里面保存的就是图片数据 //3保存为文件
MS.Clear;
MS.WriteBuffer(pData^,iLen);
MS.SaveToFile(ExtractFilePath(Application.ExeName) +sFN);
finally
MS.Free;
end;
end;
end; procedure TForm1.FormCreate(Sender: TObject);
begin
IdHTTPServer1.DefaultPort:= ;
IdHTTPServer1.Active:= true;
Self.Caption:= '启动';
end; end.
最新文章
- SqlServer性能优化(一)
- Android UI 绘制过程浅析(三)layout过程
- mac上抓iphone数据包
- [Xamarin] 透過 intent-filter 來接管 http ,製作偽瀏覽器 (转帖)
- Thinkphp中验证码的使用以及验证的实现
- yii2构造方法
- flappy pig小游戏源码分析(1)——主程序初探
- 嵌入式linux平台搭建
- [C#][转]与MySql连接访问
- Matlab与.NET基于类型安全的接口混合编程入门
- Linux svn服务的搭建
- POI tools 参数化生成excel表格
- c++模板使用及实现模板声明定义的分离
- HTML中的坐标系及其在MouseEvent和元素Box中的应用
- Python学习日记 --day3
- 从零开始学 Spring Boot
- ASP.NET Core StaticFiles中间件修改wwwroot(转载)
- PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
- Django 之单个mysql表使用
- python小数据池概念以及具体范围