本文介绍了如何使用数据库中的超链接显示gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,





我在BLOG格式的mysql数据库中存储pdf文件。



i希望在数据库中显示gridview中的pdf超链接,点击超链接,其各自的pdf文档必须显示在其他页面中。请帮我这样做。

hi all,


i'm storing pdf documents in BLOB format in mysql database.

i want to display pdf hyperlinks in gridview from database and on click of the hyperlink its respective pdf document has to be displayed in other page.please help me to do this.

推荐答案

<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="UploadFile" />
<hr />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText = "No files uploaded">
    <Columns>
        <asp:BoundField DataField="Text" HeaderText="File Name" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkDownload" Text = "Download" CommandArgument = '<%# Eval("Value") %>' runat="server" OnClick = "DownloadFile"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID = "lnkDelete" Text = "Delete" CommandArgument = '<%# Eval("Value") %>' runat = "server" OnClick = "DeleteFile" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>





















Default.aspx.cs文件:



protected void Page_Load(object sender,EventArgs e)

{

if(!IsPostBack)

{

string [] filePaths = Directory.GetFiles(Server.MapPath(〜/ Uploads / ));

列表< listitem> files = new List< listitem>();

foreach(filePaths中的字符串filePath)

{

files.Add(new ListItem(Path) .GetFileName(filePath),filePath));

}

GridView1.DataSource = files;

GridView1.DataBind();

}

}











Default.aspx.cs File :

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();
}
}

protected void UploadFile(object sender, EventArgs e)
{
    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
    Response.Redirect(Request.Url.AbsoluteUri);
}










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();
}










protected void DeleteFile(object sender, EventArgs e)
{
    string filePath = (sender as LinkButton).CommandArgument;
    File.Delete(filePath);
    Response.Redirect(Request.Url.AbsoluteUri);
}


这篇关于如何使用数据库中的超链接显示gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 22:03