• idhttp文件的上传和下载


    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[1],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[1],length(sData));
          MS.Position:=0;
          MS.ReadBuffer(iLen,sizeof(iLen)); //读文件名长度
          SetLength(sFN,iLen);
          MS.ReadBuffer(sFN[1],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:= 2111;
      IdHTTPServer1.Active:= true;
      Self.Caption:= '启动';
    end;
    
    end.
    书搞进脑袋 创新 创造; 积极
  • 相关阅读:
    面试题:垂直居中几种方法
    零碎记忆--随机
    v-model 和 :model 的区别
    Vue--keep-alive 理解
    vue 中 $route 和 $router 的区别
    Vue + Element UI 实现权限管理系统 前端篇(十):动态加载菜单
    Vue + Element UI 实现权限管理系统 前端篇(九):接口格式定义
    Vue + Element UI 实现权限管理系统 前端篇(八):管理应用状态
    Vue + Element UI 实现权限管理系统 前端篇(七):功能组件封装
    [2020牛客暑期多校训练营(第十场)C Decrement on the Tree]
  • 原文地址:https://www.cnblogs.com/tobetterlife/p/12169477.html
Copyright © 2020-2023  润新知