ASP.NET - how to view file from gridviews - Asked By sunil pandey on 16-Feb-12 02:27 AM

Hi all

i want to view the file not download the file is in folder

i written this.which is downloading the file but i want to view only

 <asp:TemplateField HeaderText="QUOTATION">
          <asp:HyperLink ID="HyperLink1" runat="server"
                     navigateUrl='<%# Eval("Doc","~/Doc/{0}")%>' Target="_blank"
                     Text="View" Visible='<%# Eval("Doc").ToString()==""?false:true %>'></asp:HyperLink>
kalpana aparnathi replied to sunil pandey on 16-Feb-12 04:18 AM

To open a file first of all you have to save file in any of the temparory location in your virtual directory. That location must be inside of your virtual directory and can accessible from internet. Now prepare virtual path of temparory saved file. and open new window with javascript and and set that temparory file path.

Also in dialy or hourly basis we have to run script that will clean all old files from temparory files.

So below are the exect stpes.

1. Create temparory location in virtual directlry like (here content is temparory location)

2. Read file from database

3. Save file in Contents folder and also check in content folder if any files available which is x hours old then delete those file

4. Prepare virtual path of the file

5. Open new popup widnow and set this path in to open file.

5. If file is not supported by browser then it promtps to download with save/open button else that will directly open in browser.

dipa ahuja replied to sunil pandey on 18-Feb-12 01:33 PM
1. Add a LinkButton with Download Command Name
<asp:Label ID="lblFileID" runat="server" Text='<%# Eval("Fileid") %>'/>
<asp:LinkButton ID="lblFileName" CommandName="Download" runat="server" Text="Download" />

2. Implement the Gridview RowCommandEvent:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  if (e.CommandName == "Download")
    GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
    string strRequest = ((Label)row.FindControl("lblFileID")).Text;
    if (strRequest != "")
      string path = Server.MapPath(strRequest); //get file object as FileInfo  
      System.IO.FileInfo file = new System.IO.FileInfo(path); //-
      if (file.Exists) //set appropriate headers  
        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
        Response.AddHeader("Content-Length", file.Length.ToString());
        Response.ContentType = "application/octet-stream";
        // write file to browser
        // if file does not exist
        Response.Write("This file does not exist.");
      //nothing in the URL as HTTP GET
      Response.Write("Please provide a file to download.");