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;

發佈了72 篇原創文章 · 獲贊 13 · 訪問量 53萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章