简体   繁体   中英

How to display and download word file from gridview?

I have developed a system to upload word file along with TITLE textbox but no idea how to display and download from gridview.

 protected void UploadTender()
    {
        try
        {
            if (FileUpload1.HasFile)
            {


                string fileName = Path.GetFileName(FileUpload1.FileName);
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/UploadedTenders/") + fileName);

                HdnFieldUploadedTender.Value = fileName;



                ResultLabel.ResultLabelAttributes("Tender Uploaded", ProjectUserControls.Enums.ResultLabel_Color.Red);
                ResultPanel.Controls.Add(ResultLabel);
            }
            else
            {
                ResultLabel.ResultLabelAttributes("No file specified", ProjectUserControls.Enums.ResultLabel_Color.Red);
                ResultPanel.Controls.Add(ResultLabel);
            }
        }
        catch (Exception ex)
        {
            ResultLabel.ResultLabelAttributes(ex.Message, ProjectUserControls.Enums.ResultLabel_Color.Red);
            ResultPanel.Controls.Add(ResultLabel);
        }
        finally { }
    }

Gridview:

 <asp:GridView runat="server" ID="grdviewUploadedTenders" OnRowCommand="grdviewUploadedTenders_RowCommand" DataKeyNames="pk_UploadedTenders_UploadedTenderID" AutoGenerateColumns="false" CssClass="table table-condensed table-bordered table-striped table-responsive">
                                        <Columns>
                                            <asp:BoundField DataField="pk_UploadedTenders_UploadedTenderID" HeaderText="Tender ID" />
                                            <asp:BoundField DataField="UploadedTenderTitle" HeaderText="Tender Title" />
                                            <asp:BoundField DataField="UploadedTenderRemarks" HeaderText="Remarks" />
                                            <asp:BoundField DataField="UploadedTenderSystemEntryDateTime" HeaderText="Uploaded On" />

                                            <asp:ButtonField CommandName="cmdEdit" ImageUrl="~/assets/global/images/shopping/edit.png" ButtonType="Image" ControlStyle-Width="25px" ControlStyle-Height="25px" />
                                            <asp:TemplateField>
                                                <ItemTemplate>
                                                    <asp:ImageButton ID="btnDelete" runat="server" ImageUrl="~/assets/global/images/shopping/delete.png"
                                                        CommandName="cmdDelete" CommandArgument='<%# Container.DataItemIndex %>' OnClientClick="return confirm('Are you Sure ?');"
                                                        ControlStyle-Width="25px" ControlStyle-Height="20px" />
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                        </Columns>
                                    </asp:GridView>

I am storing file path in sql TABLE. It's been stored there but issue is getting and making it able to be downloaded.

Check this tutorial

Displaying the files from folder or directory in ASP.Net GridView

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
        List<ListItem> files = new List<ListItem>();
        foreach (string filePath in filePaths)
        {
            files.Add(new ListItem(Path.GetFileName(filePath), filePath));
        }
        GridView1.DataSource = files;
        GridView1.DataBind();
    }
}

Downloading the Uploaded File from ASP.Net GridView

protected void DownloadFile(object sender, EventArgs e)
{
    string filePath = (sender as LinkButton).CommandArgument;
    Response.ContentType = ContentType;
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
    Response.WriteFile(filePath);
    Response.End();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM