单击时如何编写下载文件的代码点击此处下载链接? [英] How to write code for download file when click on Click here to download link ?
本文介绍了单击时如何编写下载文件的代码点击此处下载链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<asp:DataList ID="DataList1" runat="server" RepeatColumns="1"
RepeatDirection="Horizontal" CellPadding="2" CellSpacing="2" GridLines="Horizontal"
ForeColor="" Font-Bold="True" Width="100%" RepeatLayout="Flow">
<ItemTemplate>
<table width="100%" style="font-family:Arial;font-weight:normal;color:#4b4b4b;font-size:12px;border-bottom:1px solid #CDCDCD;"><tr><td style="width:20%"><b>Class:</b></td><td>
<asp:Label ID="ClassLabel" runat="server" Text='<%# Eval("Class") %>' />
</td></tr>
<tr><td>
<b>Subject:</b></td><td>
<asp:Label ID="SubjectLabel" runat="server" Text='<%# Eval("Subject") %>' />
</td></tr>
<tr><td><b>Topic:</b></td><td>
<asp:Label ID="TopicLabel" runat="server" Text='<%# Eval("Topic") %>' />
</td></tr>
<tr><td>
<b>Description:</b></td><td>
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />
<asp:LinkButton ID="lbtnd" runat="server" OnClick="lbtnd_Click">Click Here To Download</asp:LinkButton>
</td></tr>
</table>
</ItemTemplate>
<SeparatorStyle BorderStyle="None" />
</asp:DataList>
< br $> b $ b
protected void lbtnd_Click(object sender, EventArgs e)
{
LinkButton lbtn = (LinkButton)sender;
topic2 = lbtn.Text;
SqlCommand cmd = new SqlCommand("select Fileurl from OE_FileUpload where Description ='" + topic2 + "'", con);
con.Open();
fileurl = cmd.ExecuteScalar().ToString();
string Vpath = "~/UploadData/" + fileurl;
string ppath = Server.MapPath(Vpath);
string name = Path.GetFileName(ppath);
string ext = Path.GetExtension(ppath);
string type = "";
if (System.IO.File.Exists(ppath))
{
if (ext == ".pdf" || ext == ".PDF")
{
type = "Application/pdf";
}
else if (ext == ".JPG" || ext == ".jpg")
{
type = "Application/jpg";
}
else if (ext == ".doc" || ext == ".DOC")
{
type = "Application/doc";
}
else if (ext == ".docx" || ext == ".DOCX")
{
type = "Application/docx";
}
else if (ext == ".xls" || ext == ".XLS")
{
type = "Application/xls";
}
else if (ext == ".xlsx" || ext == ".XLSX")
{
type = "Application/xlsx";
}
else if (ext == ".zip" || ext == ".ZIP")
{
type = "application/x-zip-compressed";
}
//else if (ext == ".rar" || ext == ".RAR")
//{
// type = "application/x-rar-compressed";
//}
else if (ext == ".rar" || ext == ".RAR")
{
type = "application/octet-stream";
}
else if (ext == ".txt" || ext == ".TXT")
{
type = "application/text";
}
if (type != "")
{
Response.Clear();
Response.ClearHeaders();
Response.ContentType = type;
Response.AppendHeader("content-disposition", "attachment; filename=" + name);
FileInfo info = new FileInfo(name);
Response.Flush();
Response.WriteFile(ppath);
// Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
else
{
}
}
推荐答案
最简单的方法是将文件放入解决方案目录和下载按钮点击事件
protected void button1_click(event args,sender d)
{
response.redirect(resourcefile.doc);
//你有什么延伸.pdf等
}
The simplest way is to put file in solution directory and on download button click event
protected void button1_click(event args,sender d)
{
response.redirect(resourcefile.doc);
// what ever extention you have .pdf etc
}
我希望它会有用
I hope it will be useful
<a href=''<%# Eval("file paht") %>''> Download </a>
感谢你
thanking you
这篇关于单击时如何编写下载文件的代码点击此处下载链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文