Quote:
string fileName = "file";
byte[] pdfasBytes = Encoding.ASCII.GetBytes(file);
Well, there's your problem. You're not reading any bytes from the specified file; you're taking the string
"file"
and getting an array of ASCII code-points representing the characters in that string.
The simplest option is to throw your code away and use the
Response.TransmitFile[
^] method instead:
string virtualPath = "~/downloads/your-file.pdf";
string physicalPath = Server.MapPath(virtualPath);
Response.ContentType = "application/pdf";
Response.Headers.Add("content-disposition", "attachment;filename=" + Path.GetFileName(physicalPath));
Response.TransmitFile(physicalPath);
If you
really want to read the file into memory first, then you'll need to actually read the file:
string virtualPath = "~/downloads/your-file.pdf";
string physicalPath = Server.MapPath(virtualPath);
byte[] pdfasBytes = System.IO.File.ReadAllBytes(physicalPath);
NB: This is horribly inefficient. Every request is going to end up storing the entire contents of the file in memory twice - once for your byte array, and once for the buffered response. Your server will very quickly run out of memory.