nz881 nz881 - 1 month ago 10
ASP.NET (C#) Question

How to VIEW files on asp.net?

I am trying to view files from a folder in asp.net. I have tried using the "Response" class and its many functions to view files but so far I have been unsuccessful. Mostly using the Response class allows me to download the files but not view them in the browser. Most of what I have seen online suggests the same thing which is to use this bit of code:

string fileName = "Myfile.pdf";
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "inline; filename="+fileName);


But again, this only allows me to download the file and not view it in the browser.

Any suggestions on how I can do this?

Answer

This is what works for me:

        Response.Clear();
        Response.AddHeader("Content-Length", binaryFile.Length.ToString(CultureInfo.InvariantCulture));
        //Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", title)); // save file as attachment
        Response.AddHeader("Content-Disposition", string.Format("inline; filename={0}", title)); // display inline in browser
        Response.AddHeader("Content-Type", "application/pdf");
        Response.BinaryWrite(binaryFile);
        Response.Flush();
        Response.End();