zoukankan      html  css  js  c++  java
  • 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.
    书搞进脑袋 创新 创造; 积极
  • 相关阅读:
    0309. Best Time to Buy and Sell Stock with Cooldown (M)
    0621. Task Scheduler (M)
    0106. Construct Binary Tree from Inorder and Postorder Traversal (M)
    0258. Add Digits (E)
    0154. Find Minimum in Rotated Sorted Array II (H)
    0797. All Paths From Source to Target (M)
    0260. Single Number III (M)
    0072. Edit Distance (H)
    0103. Binary Tree Zigzag Level Order Traversal (M)
    0312. Burst Balloons (H)
  • 原文地址:https://www.cnblogs.com/tobetterlife/p/12169477.html
Copyright © 2011-2022 走看看