0

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?

1
  • 1
    Whether a file can be viewed or not is dependent on the browser having the capability to view it. If the browser can't, and there is no relevant plugin/extension, then a library will be needed to turn the file into a representation that the browser can understand (HTML/CSS/JS or some other format the browser can handle natively or via plugin), or the file will need to be downloaded to the user's machine. Commented Oct 31, 2016 at 20:36

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

Comments

0

Here is some nice project using free HTML based file browser:

https://github.com/magicbruno/FileBrowser

Alternative would be to use something like ASP.NET GridView and handle browsing specific folder. Here is short sample.

ASPX code:

<asp:GridView ID="GridView1" runat="server"></asp:GridView>

Page code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DisplayDirectoryContent(Server.MapPath("~"));
    }
}

void DisplayDirectoryContent(string directory)
{
    System.Data.DataTable data = new System.Data.DataTable();
    data.Columns.Add("Name",typeof(string));
    data.Columns.Add("IsFolder", typeof(bool));
    foreach (var dir in System.IO.Directory.GetDirectories(directory))
    {
       System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(directory);
       data.Rows.Add(new object[] { di.Name, true });
    }
    foreach (var file in System.IO.Directory.GetFiles(directory))
    {
        System.IO.FileInfo fi = new System.IO.FileInfo(file);
        data.Rows.Add(new object[] { fi.Name, false });
    }
    GridView1.DataSource = data;
    GridView1.DataBind();
}

Take notice that you cant directly view any file in browser, but you can link downolad action to GridView (similar to your code). This is how it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.