shelf icon indicating copy to clipboard operation
shelf copied to clipboard

how to safety close file stream

Open MisterChangRay opened this issue 1 year ago • 6 comments

i wanna write a file download server.

as Response code as following

        return Response(
          200,
          body: File(file.path!).openRead(),
          headers: headers,
        );

as u see, i send file as stream for return;

but, i has a proplem, on browser download large file , if user cancel download process, the file has never be closed!

so, how to get the socket error for close the file stream?

MisterChangRay avatar Feb 03 '24 15:02 MisterChangRay

i saw the code in file shelf_io.dart at line 254

  return httpResponse
      .addStream(response.read())
      .then((_) => httpResponse.close());

this then call be stream read end, if exception has occur, the call not be execute.

and i search on google, find the HttpResonse has a done method, this method has called by any case;

so , can expose the httpResonse.done method?

MisterChangRay avatar Feb 03 '24 16:02 MisterChangRay

@MisterChangRay – ooo...this might be a bug, honestly!

kevmoo avatar Feb 03 '24 21:02 kevmoo

@MisterChangRay – ooo...this might be a bug, honestly!

can fixed the bug in next version?

MisterChangRay avatar Feb 05 '24 01:02 MisterChangRay

@kevmoo

MisterChangRay avatar Mar 09 '24 01:03 MisterChangRay

i need too, tk

wengxianxun avatar Mar 10 '24 14:03 wengxianxun

I think #399 is same issue.

pedia avatar Mar 21 '24 03:03 pedia