I have one method to download s3 file
Method
Public ActionResult download(string filename, string credentials)
{
....
Using(stream res = response from s3)
{
return file(res, type, filename);
}
}
But exception is throwing on executing the above method.
Exception message - The request was aborted: The connection was closed unexpectedly
I have to release the stream 'res' object after download.
In the case of the File method for returning an ActionResult, you are transferring responsibility for closing the stream to the action-result, so indeed you do not want to call Dispose or use using. This is so that the ActionResult does not need to buffer the data. So: just take out the using:
public ActionResult Download(string filename, string credentials)
{
....
var res = /* response from s3 */
return File(res, type, filename);
}
If you have non-trivial code, you can make it more complex:
public ActionResult Download(string filename, string credentials)
{
....
Stream disposeMe = null;
try {
// ...
disposeMe = /* response from s3 */
// ...
var result = File(disposeMe, type, filename);
disposeMe = null; // successfully created File result which
// now owns the stream, so *leave stream open*
return result;
} finally {
disposeMe?.Dispose(); // if not handed over, dispose
}
}