Показать сообщение отдельно
  #3  
Старый 30.11.2012, 20:09
Snake22 Snake22 вне форума
Активный
 
Регистрация: 20.02.2011
Сообщения: 374
Репутация: 744
По умолчанию

удалось нагуглить вот такой код.но в нём есть две строчки которые навероне изза инди10 не работают.
сторчки эти в конце кода,
первая:
FileStrm := TRangeFileStream(FileName,ARequestInfo.ContentRang eStart, ARequestInfo.ContentRangeEnd);
у меня почему-то можно передать только один параметр (FileName) а на остальные два пишет типа не требуются.
(Ошибка [Error] Unit1.pas(122): ')' expected but ',' found )
и вторая строчка,
AThread.Connection.WriteBuffer(LBuf, LNumRead);
в инди10 вместо Athread используется AContext и функции Connection.WriteBuffer нету тут.
Код:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPServer, IdCustomHTTPServer,
  IdHTTPServer, StdCtrls,IdContext,math;



type
  TForm1 = class(TForm)
    IdHTTPServer1: TIdHTTPServer;
    Memo1: TMemo;
    Edit1: TEdit;
    procedure IdHTTPServer1CommandGet(AContext: TIdContext;
      ARequestInfo: TIdHTTPRequestInfo;
      AResponseInfo: TIdHTTPResponseInfo);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

TRangeFileStream = class(TFileStream)
private
FRangeStart, FRangeEnd: Cardinal;
FRangeEnabled: Boolean;
function GetAbsolutePosition: Longint;
protected
property AbsolutePosition: Longint read GetAbsolutePosition;
public
constructor Create(const AFileName: String; ARangeStart, ARangeEnd:
Cardinal);
function Read(var Buffer; Count: Longint): Longint; override;
function Seek(Offset: Longint; Origin: Word): Longint; override;
property RangeStart: Cardinal read FRangeStart;
property RangeEnd: Cardinal read FRangeEnd;
end;

var
  Form1: TForm1;



implementation

{$R *.dfm}

constructor TRangeFileStream.Create(const AFileName: String;  ARangeStart, ARangeEnd: Cardinal);
var
LSize: Longint;
begin
inherited Create(AFileName, fmOpenRead or fmShareDenyWrite);
LSize := Self.Size;
if ARangeStart > LSize then begin
ARangeStart := LSize;
end;
if ARangeEnd > LSize then begin
ARangeEnd := LSize;
end;
FRangeEnabled := (ARangeStart <= ARangeEnd) and (ARangeEnd <> 0);
if FRangeEnabled then begin
FRangeStart := ARangeStart;
FRangeEnd := ARangeEnd;
end;
end;


function TRangeFileStream.GetAbsolutePosition: Longint;
begin
Result := inherited Seek(0, soFromCurrent);
end;

function TRangeFileStream.Read(var Buffer; Count: Longint): Longint;
var
LBytesLeft: Cardinal;
begin
if FRangeEnabled then begin
LBytesLeft := (FRangeEnd+1) - AbsolutePosition;
if Count > LBytesLeft then begin
Count := LBytesLeft;
end;
end;
Result := inherited Read(Buffer, Count);
end;

function TRangeFileStream.Seek(Offset: Longint; Origin: Word): Longint;
var
LPos: Longint;
begin
if FRangeEnabled then begin
Case Origin of
soFromBeginning: Offset := FRangeStart + Offset;
soFromCurrent: Offset := AbsolutePosition + Offset;
soFromEnd: Offset := (FRangeEnd+1) + Offset;
end;
Offset := Max(Offset, FRangeStart);
Offset := Min(Offset, FRangeEnd+1);
Origin := soFromBeginning;
end;
Result := inherited Seek(Offset, Origin);
end;
//============

procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);

var
FileStrm: TRangeFileStream;
LSize, LNumRead: Integer;
LBuf: array[0..1023] of Byte;
FileName:string;
begin
//...
FileStrm := TRangeFileStream(FileName,
ARequestInfo.ContentRangeStart, ARequestInfo.ContentRangeEnd); 
try
LSize := FileStrm.Size;

AResponseInfo.ContentType :='...'; //
AResponseInfo.ContentLength := LSize;
AResponseInfo.ContentRangeStart := FileStrm.RangeStart;
AResponseInfo.ContentRangeEnd := FileStrm.RangeEnd;
AResponseInfo.WriteHeader;

FileStrm.Position := 0;
while FileStrm.Position < LSize do
begin
LNumRead := FileStrm.Read(LBuf, Sizeof(LBuf));
if (LNumRead > 0) then begin
AThread.Connection.WriteBuffer(LBuf, LNumRead); 
end;
end;
finally
FileStrm.Free;
end;
end;

end.
Ответить с цитированием