I am sending a file to client for download. File could be big (upto few GBs), so I am sending it in chunks. This is my code:
//code to get data in resp stream.
using (Stream inputStream = resp.GetResponseStream())
{
if (System.IO.File.Exists(TargetFile))
{
System.IO.File.Delete(TargetFile);
}
using (FileStream fs = System.IO.File.Open(TargetFile, FileMode.Create, FileAccess.ReadWrite))
{
byte[] buffer = new byte[SegmentSize];
int bytesRead;
while ((bytesRead = inputStream.Read(buffer, 0, SegmentSize)) > 0)
{
fs.Write(buffer,0,bytesRead);
}
Response.AddHeader("Content-Disposition", "Attachment;filename=targetFileName.pdf");
return File(fs, "application/pdf");
}
}
When I click on the link to download it gives me above error. I tried with returning nothing and in that case it downloads the file but size of the file is zero. The error is in the last line.