Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 如何删除MemoryStream中的起始头并将其余头保存到文件中?_File_Delphi - Fatal编程技术网

File 如何删除MemoryStream中的起始头并将其余头保存到文件中?

File 如何删除MemoryStream中的起始头并将其余头保存到文件中?,file,delphi,File,Delphi,我目前有一个TMemoryStream,它通过读取varbinary字段来填充,varbinary字段最初是由UTF8标准从mssqlserverdb表中编码的 varbinary字段包含一个头部分,在将流的其余部分保存为文件之前需要删除该头部分 我已经成功地提取了所需的头信息,其中包括文件名,但还没有 找到一个解决方案,然后将流的其余部分保存为有效文件 数据最初读入TdxMemData数据集 程序TBaseDataPump.SaveAttachments(mtblAttachmentInfo:

我目前有一个
TMemoryStream
,它通过读取varbinary字段来填充,varbinary字段最初是由UTF8标准从mssqlserverdb表中编码的

varbinary字段包含一个头部分,在将流的其余部分保存为文件之前需要删除该头部分

我已经成功地提取了所需的头信息,其中包括文件名,但还没有 找到一个解决方案,然后将流的其余部分保存为有效文件

数据最初读入
TdxMemData
数据集

程序TBaseDataPump.SaveAttachments(mtblAttachmentInfo:TdxMemData);
变量
AFile:TMemoryStream;
ANewFile:TStringStream;
sFullPath:字符串;
wsImageBlob,wsHeader,wsBody:WideString;
i位置:整数;
开始
如果mtblAttachmentInfo.Active,则//TdxMemData填充数据
开始
使用mtblAttachmentInfo do
开始
第一;
而不是EOF做什么
开始
//下面的代码可以在不删除数据的情况下保存文件
AFile:=TMemoryStream.Create;//创建内存流
TBlobField(FieldByName('ImageBlob')).SaveToStream(AFile);
//将文件映像从数据加载到内存strem
//获取标题内容
ANewFile:=TStringStream.Create(“”);
TBlobField(FieldByName('ImageBlob')).SaveToStream(ANewFile);
//将该文件再次读取到TStringStream
wsImageBlob:=UTF8ToString(ANewFile.DataString);
//我可以将数据视为宽字符串,并可以在屏幕上找到标题位及其结束位置
//它在字符串上的结束位置
i位置:=AnsiPos(#13#13#10,wsImageBlob);//查找标题结束位置
//将标头获取到另一个宽字符串
wsHeader:=AnsiLeftStr(wsImageBlob,位置);
wsHeader:=AnsiLeftStr(wsImageBlob,POS(#9,wsHeader)-1);
wsHeader:=StringReplace(wsHeader,“”,,[rfReplaceall]);
//需要将来自头文件末尾位置的流保存为文件
结束;
结束;
结束;
结束;

使用copyfrom方法找到了解决方案。其内容如下:

      // Create the  TStringStream
      ANewFile := TStringStream.Create('');

      try
        TBlobField(FieldByName('ImageBlob')).SaveToStream(ANewFile);

        // get the blob as a string so we can extract the header information
        wsImageBlob := UTF8ToString(ANewFile.DataString);

        position := AnsiPos(#13#13#10, wsImageBlob);

        if position <> 0 then begin
          // have header information to extract
          position := position + 2;

          wsHeader := AnsiLeftStr(wsImageBlob, position);
          wsHeader := AnsiLeftStr(wsImageBlob, POS(#9, wsHeader) - 1);
          wsHeader := StringReplace(wsHeader, '''', '', [rfReplaceall]);

          sFilePath := sDirectorypath + wsHeader;

          ANewFile.Position := position;     

          AFile := TMemoryStream.Create;
          try
            AFile.CopyFrom(ANewFile, ANewFile.Size - position);

            AFile.SaveToFile(sFilePath);
          finally
            FreeAndNil(AFile);
          end;
        end
      finally
        FreeAndNil(ANewFile);
      end;
//创建TStringStream
ANewFile:=TStringStream.Create(“”);
尝试
TBlobField(FieldByName('ImageBlob')).SaveToStream(ANewFile);
//将blob获取为字符串,以便提取标题信息
wsImageBlob:=UTF8ToString(ANewFile.DataString);
职位:=AnsiPos(#13#13#10,wsImageBlob);
如果位置为0,则开始
//有要提取的标题信息
位置:=位置+2;
wsHeader:=AnsiLeftStr(wsImageBlob,位置);
wsHeader:=AnsiLeftStr(wsImageBlob,POS(#9,wsHeader)-1);
wsHeader:=StringReplace(wsHeader,“”,,[rfReplaceall]);
sFilePath:=sDirectorypath+wsHeader;
ANewFile.Position:=位置;
AFile:=TMemoryStream.Create;
尝试
AFile.CopyFrom(ANewFile,ANewFile.Size-position);
AFile.SaveToFile(sFilePath);
最后
FreeAndNil(AFile);
结束;
结束
最后
FreeAndNil(ANewFile);
结束;

使用copyfrom方法找到了解决方案。其内容如下:

      // Create the  TStringStream
      ANewFile := TStringStream.Create('');

      try
        TBlobField(FieldByName('ImageBlob')).SaveToStream(ANewFile);

        // get the blob as a string so we can extract the header information
        wsImageBlob := UTF8ToString(ANewFile.DataString);

        position := AnsiPos(#13#13#10, wsImageBlob);

        if position <> 0 then begin
          // have header information to extract
          position := position + 2;

          wsHeader := AnsiLeftStr(wsImageBlob, position);
          wsHeader := AnsiLeftStr(wsImageBlob, POS(#9, wsHeader) - 1);
          wsHeader := StringReplace(wsHeader, '''', '', [rfReplaceall]);

          sFilePath := sDirectorypath + wsHeader;

          ANewFile.Position := position;     

          AFile := TMemoryStream.Create;
          try
            AFile.CopyFrom(ANewFile, ANewFile.Size - position);

            AFile.SaveToFile(sFilePath);
          finally
            FreeAndNil(AFile);
          end;
        end
      finally
        FreeAndNil(ANewFile);
      end;
//创建TStringStream
ANewFile:=TStringStream.Create(“”);
尝试
TBlobField(FieldByName('ImageBlob')).SaveToStream(ANewFile);
//将blob获取为字符串,以便提取标题信息
wsImageBlob:=UTF8ToString(ANewFile.DataString);
职位:=AnsiPos(#13#13#10,wsImageBlob);
如果位置为0,则开始
//有要提取的标题信息
位置:=位置+2;
wsHeader:=AnsiLeftStr(wsImageBlob,位置);
wsHeader:=AnsiLeftStr(wsImageBlob,POS(#9,wsHeader)-1);
wsHeader:=StringReplace(wsHeader,“”,,[rfReplaceall]);
sFilePath:=sDirectorypath+wsHeader;
ANewFile.Position:=位置;
AFile:=TMemoryStream.Create;
尝试
AFile.CopyFrom(ANewFile,ANewFile.Size-position);
AFile.SaveToFile(sFilePath);
最后
FreeAndNil(AFile);
结束;
结束
最后
FreeAndNil(ANewFile);
结束;

创建文件流(目标)将源流的
位置设置为开始复制的位置,然后使用源流的
CopyTo
方法复制到目标。顺便说一句,你的代码似乎太复杂了。为什么你创建一个
TMemoryStream
只是为了把它复制到
TStringStream
而不是直接从blob字段复制到
TStringStream
?你不能删除一个起始位,只能删除一个起始字节。你好,肯,谢谢你的回复。我正在使用Delphi Tokyo,但找不到与TMemoryStream、TStringStream、TFileStream或TStream关联的CopyTo方法。有一个CopyFrom方法,它给出了一个“流读取错误”。正如在评论中提到的,我使用TMemoryStream将数据提取为TBlobField并将其另存为文件。我使用TStringStream查找头部分(包含文件名和扩展名)及其位置。只要我可以删除标题部分并将其保存为文件,我就可以选择任何选项。请注意,头段长度可能会有所不同。您可以创建文件流(目标),将源流的
位置设置为要开始复制的位置,然后使用源流的
CopyTo
方法复制到目标。顺便说一句,您的代码似乎过于复杂。为什么创建
TMemoryStream
只是为了将其复制到
TStringStream
,而不是直接从blob字段复制到
TStringStream
?您不能