admin

[网络类] idhttp post 上传文件到 php

unit Unit1;


interface


uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,

  IdHTTP, StdCtrls,IdMultipartFormData, ComCtrls,SuperObject;


type

  TForm1 = class(TForm)

    edt1: TEdit;

    Button1: TButton;

    idhtp1: TIdHTTP;

    Button2: TButton;

    edt2: TEdit;

    dlgOpen1: TOpenDialog;

    pb1: TProgressBar;

    procedure Button1Click(Sender: TObject);

    procedure FormCreate(Sender: TObject);

    procedure Button2Click(Sender: TObject);

    procedure idhtp1WorkBegin(Sender: TObject; AWorkMode: TWorkMode;

      const AWorkCountMax: Integer);

    procedure idhtp1Work(Sender: TObject; AWorkMode: TWorkMode;

      const AWorkCount: Integer);

  private

    { Private declarations }

  public

    { Public declarations }

  end;


var

  Form1: TForm1;


implementation


{$R *.dfm}

uses MsMultiPartFormData;


procedure TForm1.Button1Click(Sender: TObject);


begin

 if (dlgOpen1.Execute) and (dlgOpen1.FileName<>'') then

    begin

      edt1.Text:=dlgOpen1.FileName;

    end;

end;


procedure TForm1.FormCreate(Sender: TObject);

begin

  hint:=GetCurrentDir+'\';


end;


procedure TForm1.Button2Click(Sender: TObject);

var

  response:TStringStream;

  MMPFDS:TMsMultiPartFormDataStream;

  jRet: ISuperObject;  //返回数据


begin

  //对于返回的json数据进行解析

  

  MMPFDS:=TMsMultiPartFormDataStream.Create;

  response:=TStringStream.Create('');

  idhtp1.Request.ContentType:=MMPFDS.RequestContentType;

  MMPFDS.AddFile('uploadfile',PChar(edt1.Text),'text/plain');

  MMPFDS.PrepareStreamForDispatch;

  MMPFDS.Position:=0;

  try

    idhtp1.Post(PChar(edt2.Text),MMPFDS,response);

  finally

    jRet := SO(response.DataString);

    showMessage(jRet.O['status'].AsString+'|'+jRet.O['content'].AsString);

    

    MMPFDS.Free;

    response.Free;

    idhtp1.Disconnect;

  end;

end;


procedure TForm1.idhtp1WorkBegin(Sender: TObject; AWorkMode: TWorkMode;

  const AWorkCountMax: Integer);

begin

  pb1.Max:=AWorkCountMax;

  pb1.Position:=0;

end;


procedure TForm1.idhtp1Work(Sender: TObject; AWorkMode: TWorkMode;

  const AWorkCount: Integer);

begin

  pb1.Position:=AWorkCount;

end;


end.


#1楼
发帖时间:4月前   |   查看数:0   |   回复数:2
admin
MsMultiPartFormData.pas

unit MsMultiPartFormData;

interface

uses
 SysUtils, Classes;

const
 CONTENT_TYPE = 'multipart/form-data; boundary=';
 CRLF = #13#10;
 CONTENT_DISPOSITION = 'Content-Disposition: form-data; name="%s"';
 FILE_NAME_PLACE_HOLDER = '; filename="%s"';
 CONTENT_TYPE_PLACE_HOLDER = 'Content-Type: %s' + crlf + crlf;
 CONTENT_LENGTH = 'Content-Length: %d' + crlf;

type
 TMsMultiPartFormDataStream = class(TMemoryStream)
 private
   FBoundary: string;
   FRequestContentType: string;
   function GenerateUniqueBoundary: string;
 public
   procedure AddFormField(const FieldName, FieldValue: string);
   procedure AddFile(const FieldName, FileName, ContentType: string; FileData: TStream); overload;
   procedure AddFile(const FieldName, FileName, ContentType: string); overload;
   procedure PrepareStreamForDispatch;
   constructor Create;
   property Boundary: string read FBoundary;
   property RequestContentType: string read FRequestContentType;
 end;

implementation
{ TMsMultiPartFormDataStream }

constructor TMsMultiPartFormDataStream.Create;
begin
 inherited;
 FBoundary := GenerateUniqueBoundary;
 FRequestContentType := CONTENT_TYPE + FBoundary;
end;

procedure TMsMultiPartFormDataStream.AddFile(const FieldName, FileName, ContentType: string; FileData: TStream);
var
 sFormFieldInfo: string;
 Buffer: PChar;
 iSize: Int64;
begin
 iSize := FileData.Size;
 sFormFieldInfo := Format(CRLF + '--' + Boundary + CRLF + CONTENT_DISPOSITION + FILE_NAME_PLACE_HOLDER + CRLF + CONTENT_LENGTH + CONTENT_TYPE_PLACE_HOLDER, [FieldName, FileName, iSize, ContentType]);
 Write(Pointer(sFormFieldInfo)^, Length(sFormFieldInfo));
 FileData.Position := 0;
 GetMem(Buffer, iSize);
 try
   FileData.Read(Buffer^, iSize);
   Write(Buffer^, iSize);
 finally
   FreeMem(Buffer, iSize);
 end;
end;

procedure TMsMultiPartFormDataStream.AddFile(const FieldName, FileName, ContentType: string);
var
 FileStream: TFileStream;
begin
 FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
 try
   AddFile(FieldName, FileName, ContentType, FileStream);
 finally
   FileStream.Free;
 end;
end;

procedure TMsMultiPartFormDataStream.AddFormField(const FieldName, FieldValue: string);
var
 sFormFieldInfo: string;
begin
 sFormFieldInfo := Format(CRLF + '--' + Boundary + CRLF + CONTENT_DISPOSITION + CRLF + CRLF + FieldValue, [FieldName]);
 Write(Pointer(sFormFieldInfo)^, Length(sFormFieldInfo));
end;

function TMsMultiPartFormDataStream.GenerateUniqueBoundary: string;
begin
 Result := '---------------------------' + FormatDateTime('mmddyyhhnnsszzz', Now);
end;

procedure TMsMultiPartFormDataStream.PrepareStreamForDispatch;
var
 sFormFieldInfo: string;
begin
 sFormFieldInfo := CRLF + '--' + Boundary + '--' + CRLF;
 Write(Pointer(sFormFieldInfo)^, Length(sFormFieldInfo));
 Position := 0;
end;

end.
4月前 #2楼
admin

upload.php

<?php
date_default_timezone_set("PRC");

   $picname = $_FILES['uploadfile']['name'];
   $picsize = $_FILES['uploadfile']['size'];
   if ($picname != "") {
       if ($picsize > 2014000) { //限制上传大小
           echo '{"status":0,"content":"文件大小不能超过2M"}';
           exit;
       }
       $type = strstr($picname, '.'); //限制上传格式
       if ($type != ".jpg" && $type != ".pdf"  && $type != ".rar" && $type != ".doc" && $type != ".docx" && $type != ".xls" && $type != ".xlsx"  && $type != ".PDF") {
           echo '{"status":2,"content":"文件格式不支持!"}';
           exit;
       }
       $rand = rand(100, 999);
       $pics = uniqid() . $type; //命名图片名称

       //上传路径 不存在就建立
                if(!file_exists("uploads/".date("Ym")."/")) {  
           mkdir("uploads/".date("Ym")."/");  
       }  

       $pic_path = "uploads/".date("Ym")."/". $pics;
       move_uploaded_file($_FILES['uploadfile']['tmp_name'], $pic_path);
   }
   $size = round($picsize/1024,2); //转换成kb
   echo '{"status":1,"name":"'.$picname.'","url":"'.$pic_path.'","size":"'.$size.'","content":"上传成功!"}';    
?>

4月前 #3楼
游客组