zoukankan      html  css  js  c++  java
  • datasnap 上传/下载大文件(本Demo以图传片文件为例)

    好久没写技术文了 datasnap传大流。 完整代码,同时感谢叶兄传流的指点,(只公开十天)

    附:下面代码,转载请注明出处

    ::code

    服务端:

    function TServerMethods1.DownLoadFile(AfileName: string): TStream;
    const
    SaveFolder = 'FSimage\';
    defaultName = 'Default.png'; // 此文件必须有
    //用默认文件处理不存在图片
    var
    ALLpath: string;
    begin
    ALLpath := LocalPath + SaveFolder + AfileName;
    if not FileExists(ALLpath) then
        ALLpath := LocalPath + SaveFolder + defaultName;
    Result := TFileStream.Create(ALLpath, fmOpenRead);
    Result.Position := 0;
    //此处也可以加错误处理 或 释放默认文件 再生成流
    end;

    function TServerMethods1.PutFile(AfileName: string; Stream: TStream): Boolean;
    const
    BufSize = $F000;
    SaveFolder = 'FSimage\';
    var
    Buffer: TBytes;
    ReadCount: Integer;
    FS: TFileStream;
    begin
    if not DirectoryExists(LocalPath + SaveFolder) then
        CreateDir(LocalPath + SaveFolder);
    FS := TFileStream.Create(LocalPath + SaveFolder + AfileName, FmCreate);
    try
        if Stream.Size = -1 then // 大小未知则一直读取到没有数据为止
        begin
          SetLength(Buffer, BufSize);
          repeat
            ReadCount := Stream.Read(Buffer[0], BufSize);
            if ReadCount > 0 then
              FS.WriteBuffer(Buffer[0], ReadCount);
            if ReadCount < BufSize then
              break;
          until ReadCount < BufSize;
        end
        else // 大小已知则直接复制数据
          FS.CopyFrom(Stream, 0);
        Result := True;
    finally
        FS.Free;
    end;
    end;

    客户端:

    procedure TForm1.Button1Click(Sender: TObject);
    var
    cs: TServerMethods1Client;
    memoryStream: TMemoryStream;
    begin
    cs := TServerMethods1Client.Create(self.SQLConnection1.DBXConnection);
    try
        memoryStream := TMemoryStream.Create;
        try
          // Image1.Picture.Graphic.SaveToStream(memoryStream);
          SaveAs(Image1.Picture.Graphic, memoryStream, gptPNG);
          memoryStream.Position := 0;
          // memoryStream.Seek(0, TSeekOrigin.soBeginning);
          if cs.PutFile('1.png', memoryStream) then
            ShowMessage('保存成功')
          else
            ShowMessage('保存失败');
        finally
          if memoryStream <> nil then
            memoryStream := nil;
        end;
    finally
        cs.free;
    end;
    end;

    procedure TForm1.Button2Click(Sender: TObject);
    begin
    DownLoadfs(Image1.Picture,'1.png')
    end;

    procedure TForm1.DownLoadfs(Ggs:TPicture;fsName: string);
    const
    BufSize = $F000;
    var
    Stream, FS: TStream;
    cs: TServerMethods1Client;
    syn: TSynPicture;
    Buffer: TBytes;
    ReadCount: Integer;
    begin
    cs := TServerMethods1Client.Create(self.SQLConnection1.DBXConnection);
    try
        Stream := cs.DownLoadFile(fsName);
        FS := TMemoryStream.Create;
        try
          if Stream.Size = -1 then // 大小未知则一直读取到没有数据为止
          begin
            SetLength(Buffer, BufSize);
            repeat
              ReadCount := Stream.Read(Buffer[0], BufSize);
              if ReadCount > 0 then
                FS.WriteBuffer(Buffer[0], ReadCount);
              if ReadCount < BufSize then
                break;
            until ReadCount < BufSize;
          end
          else // 大小已知则直接复制数据
            FS.CopyFrom(Stream, 0);
          syn := TSynPicture.Create;
          try
            syn.LoadFromStream(FS);
            Ggs.Assign(syn);
          finally
            syn.free;
          end;
        finally
          FS.free;
        end;
    finally
        cs.free;
    end;

    end;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    self.SQLConnection1.Connected := True;
    gdip := TGDIPlusFull.Create('gdiplus.dll');
    end;

    procedure TForm1.Image1DblClick(Sender: TObject);
    var
    syn: TSynPicture;
    begin
    if self.OpenDialog1.Execute then
    begin
        if self.OpenDialog1.FileName = '' then
          Exit
        else
        begin
          syn := TSynPicture.Create;
          try
            syn.LoadFromFile(self.OpenDialog1.FileName);
            self.Image1.Picture.Assign(syn);
          finally
            syn.free;
          end;
        end;
    end;
    end;

    附:上面代码,转载请注明出处

    http://hi.baidu.com/vbz007/blog/item/68957181d2b9bbd7bc3e1e89.html

  • 相关阅读:
    CSS Grid 布局
    sublime text 3配置c/c++编译环境
    [工具/PC]计算机中丢失libiconv-2.dll,丢失libintl-8.dll,无法定位程序输入点libiconv于动态链接库libiconv-2.dll上问题解决方法
    操作系统Unix、Windows、Mac OS、Linux的故事
    编译型语言与解释型语言的区别及各自的优缺点
    node版本控制之nvm
    Webpack devServer中的 proxy 实现跨域
    webpack-dev-server配置指南(使用webpack3.0)
    [bzoj3295][Cqoi2011]动态逆序对_主席树
    [bzoj2989]数列_KD-Tree_旋转坐标系
  • 原文地址:https://www.cnblogs.com/sunsoft/p/1966291.html
Copyright © 2011-2022 走看看