簡體   English   中英

TFileStream創建的文件丟失寫入其中的數據

[英]TFileStream created file missing data written to it

我已經編寫了客戶機/服務器代碼,該代碼通過Delphi編寫的WebService在系統之間來回移動文件。 由於服務前網關的限制,我被迫將大文件拆分為多個消息。 下面的代碼經常在我的測試中工作。 但是,有時在最后一條數據上,似乎沒有將其寫入結果文件中。 我有另一個版本的代碼,在每個步驟中都添加了廣泛的日志記錄,以在writeBuffer調用之前和之后驗證FileStream中的當前位置,並獲取中間文件的大小。 那個版本的代碼似乎每次都起作用,這對我來說讓我認為我可能遇到某種計時問題。 每次寫入或類似操作之后,我應該在Stream上執行Flush嗎?

 responseObj := DocSvc.getDocument(GetFileInHeader, GetFileIn);
      while processingGetFile do
      begin
        chunkID := StrToInt(responseObj.ValidationResults[0].ValidationResultId);

        if chunkID = -3 then
        begin
          processingGetFile := false;
          break;
        end;
        if chunkID = -2 then
        begin
          if responseObj.opsResponseobj.status then
          begin
            if responseObj.opsResponseObj.document <> 'NONEWFILE' then
            begin
              if FileExists2(DesintationFilePath) then
                DeleteFile(DesintationFilePath);
              Zipfile := TFileStream.Create(DesintationFilePath,FmOpenReadWrite or FmShareDenyNone or fmCreate);
              DecodedZipfile := DecodeString(responseObj.opsResponseobj.document);
              Zipfile.WriteBuffer(Pointer(DecodedZipfile)^, length(DecodedZipfile));

              iniTransmit.WriteString(‘DocumentSection’,string(FileID), responseObj.ValidationResults[0].ReasonCode);
              FreeAndNil(Zipfile);
            end;
            result := true;
            processingGetFile := false;
          end
          else
          begin
            //Log failure
          end;
        end
        else if chunkID = -1 then
        begin

          Zipfile.Position := getFileSize(DesintationFilePath);
          DecodedZipfile := DecodeString(responseObj.opsResponseObj.document);

          Zipfile.WriteBuffer(Pointer(DecodedZipfile)^, Length(DecodedZipfile));
 
          iniTransmit.WriteString(‘DocumentSection’,string(FileID), responseObj.ValidationResults[0].ReasonCode);
          result := true;
          processingGetFile := false;
        end
        else // in the middle of receiving pieces of a big file. Save off what we have, ask for more.
        begin
          if chunkID = 1 then
          begin
            if FileExists2(DesintationFilePath) then
              DeleteFile(DesintationFilePath);
            Zipfile := TFileStream.Create(DesintationFilePath,FmOpenReadWrite or FmShareDenyNone or fmCreate);
          end;

          Zipfile.Position := getFileSize(DesintationFilePath);
          DecodedZipfile := DecodeString(responseObj.opsResponseObj.document);

          Zipfile.WriteBuffer(Pointer(DecodedZipfile)^, Length(DecodedZipfile));

          GetFileInHeader.messageFlowSequence := chunkID;
          responseObj := DocSvc.getDocument(GetFileInHeader, GetFileIn);
        end;
      end;


function getFileSize(path: string): integer;
var
  info : TWin32FileAttributeData;
begin
  result := -1;
 
  if not GetFileAttributesex(Pchar(path), GetFileExInfoStandard, @info) then
    exit;
 
  result := (info.nFileSizeLow or (info.nFileSizeHigh shl 32));
end;

您的問題似乎與是否需要執行以下操作才能寫入文件一樣:

Stream := TFileStream.Create(...);
Try
  Stream.WriteBuffer(...);
Finally
  Stream.Free;
End;

答案是不。 無需沖洗任何東西。

您的問題可能是由於您使用的共享模式。 您已經去過fmShareDenyNone 這意味着可以打開具有寫訪問權的多個文件句柄。 這意味着您可以進行文件編寫競賽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM