Delphi 把客户端的文件或者目录上传到服务器端

来源:互联网 发布:淘宝网电影票团购 编辑:程序博客网 时间:2024/05/21 17:20
 

1、StringReplace字符串替换函数:

function StringReplace (const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;

rfReplaceAll:全部替换
rfIgnoreCase:忽略大小写


   aStr := 'This is a book, not a pen!';

   //This is two book, not a pen!只替换了第一个符合的字

   StringReplace (aStr, 'a', 'two', []);   

   //This is two book, not two pen!替换了所有符合的字

   StringReplace (aStr, 'a', 'two', [rfReplaceAll]);   

  aStr := 'This is a book, not A pen!';

   //This is two book, not A pen!只替换了符合的字(小写a)

  StringReplace (aStr, 'a', 'two', [rfReplaceAll]);   

   //This is two book, not two pen!不管大小写替换了所有符合的字

 StringReplace (aStr, 'a', 'two', [rfReplaceAll, rfIgnoreCase]);

 

2、将客服端的ListBox中的文件或目录名称上传到服务器

//将上传文件列表中的文件或目录统一上传到服务器中对应的目录下

Procedure UpFile;

var

  i:Integer;

begin

  if CheckBox1.Checked then  // 选择了上传文件

  begin

    if ListBox1.Count > 0 then  //  listBox1中存在文件需要上传

    begin

      for i := 0 to ListBox1.Count -1 do

      begin

        // 在磁盘上存在这样的文件

        if FileExists(ListBox1.Items[i]) then

          // 使用文件上传的方式上传文件

          CopyFile(ExtractFilePath(ListBox1.Items[i]),ListBox1.Items[i])

        Else

          // 使用目录上传的方式上传文件

          CopyFileDir(ExtractFilePath(ListBox1.Items[i]),ListBox1.Items[i]);

      end;

    end;

  end;

end;

 

3、单个文件上传

Procedure CopyFile(ParentPath, Path: string);

var

  vPath:string;

  AllPath:string;

  FileStream:TFileStream;

begin

  if FileExists(Path) then

  begin

    // 等到文件的名称

vPath := StringReplace(Path,ParentPath,'\',[rfReplaceAll]);

    // 设置文件存放的路径

    AllPath := IntToStr(FYear) + '\' + IntToStr(FMonth) +

        '\' + IntToStr(FDay) + '\' + IntToStr(FUserID) + vPath;

// 把要上传的文件存入流中

FileStream := TFileStream.Create(Path,fmOpenRead);

// 使用服务器端的上传函数上传文件到服务器。GetServer是返回一个远程数据服务类。

    GetServer.UpFile(AllPath,StreamToVariant(FileStream));// 转换成变体的形式进行传递

    FileStream.Free;  // 切记

  end

end;

 

4、目录上传

Procedure CopyFileDir(ParentPath:string;Path: string);

var

  sr:TSearchRec;

  fr:Integer;

  AllPath:string;

  vPath:string;

begin

  if path[length(path)]<>'\' then

     path := path + '\';

  fr:=FindFirst(Path+'*.*',faAnyFile,sr);

  while fr=0 do

  begin

    Application.ProcessMessages;

    if (sr.Name<>'.')and(sr.Name<>'..') then

    begin

      if FileExists(Path + sr.Name) then

        CopyFile(ParentPath,Path + sr.Name)

      else

        CopyFileDir(ParentPath,Path + sr.Name);

    end;

    fr := FindNext(sr);

  end;

  Windows.FindClose(fr);

end;

 

5、服务器端的上传函数;参数为文件路径、文件数据流。

Function UpFile(Path, Stream: OleVariant): OleVariant;

var

  AllPath:string;

  FileStream:TFileStream;

begin

  Result := False;

  AllPath := ExtractFilePath(Application.ExeName) + 'WorkLog\' + Path;

  if not DirectoryExists(ExtractFilePath(AllPath)) then

    ForceDirectories(ExtractFilePath(AllPath));  //创建多层目录.

  if FileExists(AllPath) then

    DeleteFile(AllPath);

  FileStream := TFileStream.Create(AllPath,fmCreate or fmOpenReadWrite);

  VariantToStream(Stream,FileStream);  // 将变体类型的数据转换成二进制流

  FileStream.Free;

  Result := True;

end;