Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As I told you in <a href="https://stackoverflow.com/questions/9476744/how-to-optimize-upload-routine-using-delphi-2010">your earlier question</a>, you have to post a <code>TStream</code> containing the remaining file data to upload. Don't use <code>TIdMultipartFormDataStream.AddFile()</code>, as that will send the entire file. Use the <code>TStream</code> overloaded version of <code>TIdMultipartFormDataStream.AddFormField()</code> instead.</p> <p>And <code>TIdHTTP</code> is not designed to respect the <code>ContentRange...</code> properties. Most of the <code>Request</code> properties merely set the corresponding HTTP headers only, they do not influence the data. That is why your edits broke it.</p> <p>Try this:</p> <pre><code>IdHttp.Head('http://localhost/_tests/resume/large-file.bin'); FileSize := IdHttp.Response.ContentLength; FileStrm := TFileStream.Create('D:\large-file.bin', fmOpenRead or fmShareDenyWrite); try if FileSize &lt; FileStrm.Size then begin FileStrm.Position := FileSize; Stream := TIdMultipartFormDataStream.Create; try with Stream.AddFormField('upload_file', 'application/octet-stream', '', FileStrm, 'large-file.bin') do begin HeaderCharset  := 'utf-8'; HeaderEncoding := '8'; end; with IdHTTP do begin with Request do begin ContentRangeStart := FileSize + 1; ContentRangeEnd := FileStrm.Size; end; Post('http://localhost/_tests/resume/t1.php', Stream); end; finally Stream.Free; end; end; finally FileStrm.Free; end; </code></pre> <p>With that said, this is a <strong>GROSS MISUSE</strong> of HTTP and <code>multipart/form-data</code>. For starters, the <code>ContentRange</code> values are in the wrong place. You are applying them to the entire <code>Request</code> as a whole, which is wrong. They would need to be applied at the <code>FormField</code> instead, but <code>TIdMultipartFormDataStream</code> does not currently support that. Second, <code>multipart/form-data</code> was 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 using <code>TIdMultipartFormDataStream</code> and just pass the file data directly to <code>TIdHTTP.Post()</code> like I <a href="https://stackoverflow.com/questions/9476744/how-to-optimize-upload-routine-using-delphi-2010">suggested earlier</a>, eg:</p> <pre><code>FileStrm := TFileStream.Create('D:\large-file.bin', fmOpenRead or fmShareDenyWrite); try IdHTTP.Post('http://localhost/_tests/upload.php?name=large-file.bin', FileStrm); finally FileStrm.Free; end; </code></pre> <p>.</p> <pre><code>IdHTTP.Head('http://localhost/_tests/files/large-file.bin'); FileSize := IdHTTP.Response.ContentLength; FileStrm := TFileStream.Create('D:\large-file.bin', fmOpenRead or fmShareDenyWrite); try if FileSize &lt; FileStrm.Size then begin FileStrm.Position := FileSize; IdHTTP.Post('http://localhost/_tests/resume.php?name=large-file.bin', FileStrm); end; finally FileStrm.Free; end; </code></pre> <p>I already <a href="https://stackoverflow.com/questions/9476744/how-to-optimize-upload-routine-using-delphi-2010">explained earlier</a> how to access the raw <code>POST</code> data in PHP.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload