I’m trying to resume an upload using indy (HTTP Post), the code looks like this (using Delphi 2010, Indy 10.4736):
IdHttp.Head('http://localhost/_tests/resume/large-file.bin');
ByteRange := IdHttp.Response.ContentLength + 1;
// Attach the file to post/upload
Stream := TIdMultipartFormDataStream.Create;
with Stream.AddFile('upload_file', 'D:\large-file.bin', 'application/octet-stream') do
begin
HeaderCharset := 'utf-8';
HeaderEncoding := '8';
end; // with
with IdHTTP do
begin
IOHandler.LargeStream := True;
with Request do
begin
ContentRangeStart := ByteRange;
ContentRangeEnd := (Stream.Size - ByteRange);
ContentLength := ContentRangeEnd;
ContentRangeInstanceLength := ContentLength;
end; // with
Post('http://localhost/_tests/resume/t1.php', Stream);
end; // with
but upload resume doesn’t work 🙁
I looked into Indy’s code, it seems that this function in IdIOHandler.pas
TIdIOHandler.Write()
always deal with complete streams/files (since the parameter ASize: TIdStreamSize seems to be always 0, which according to the code means sending the full file/stream).
This prevents indy from resuming the upload.
My question is: is it possible to avoid sending the full file?
Setting content range didn’t change anything. I also tweaked indy’s code (modified 3 lines) to make indy obey to the content range / stream position, but it’s buggy and indy always end up hanging in IdStackWindows.pas because of an infinite timeout here:
TIdSocketListWindows.FDSelect()
As I told you in your earlier question, you have to post a
TStreamcontaining the remaining file data to upload. Don’t useTIdMultipartFormDataStream.AddFile(), as that will send the entire file. Use theTStreamoverloaded version ofTIdMultipartFormDataStream.AddFormField()instead.And
TIdHTTPis not designed to respect theContentRange...properties. Most of theRequestproperties merely set the corresponding HTTP headers only, they do not influence the data. That is why your edits broke it.Try this:
With that said, this is a GROSS MISUSE of HTTP and
multipart/form-data. For starters, theContentRangevalues are in the wrong place. You are applying them to the entireRequestas a whole, which is wrong. They would need to be applied at theFormFieldinstead, butTIdMultipartFormDataStreamdoes not currently support that. Second,multipart/form-datawas not designed to be used like this anyway. It is fine for uploading a file from the beginning, but not for resuming a broken upload. You really should stop usingTIdMultipartFormDataStreamand just pass the file data directly toTIdHTTP.Post()like I suggested earlier, eg:.
I already explained earlier how to access the raw
POSTdata in PHP.