1. 程式人生 > >Delphi 把客戶端的檔案或者目錄上傳到伺服器端

Delphi 把客戶端的檔案或者目錄上傳到伺服器端

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;