请问怎么用HyperLinkField可以直接链接数据库的文件路径字段进行文件下载 请问:用fileupload把文件上传到服务器的同时,将文件的保存地址插入数据库,然后用HyperLinkField直接链接数据库中保存的路径,最后在页面上点击HyperLinkField的字段可以下载之前上传到服务器上的文件吗?要怎么用HyperLinkField直接链接到数据库的路径字段呢?麻烦各位指点~~~~~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果是rar可以直接下载,如果是浏览器可以显示的文件,需要进行代码下 可以的,下载的本质就是进行文件的复制而已! 只是获得的文件类型不同,浏览器有不同的行为而已。如一个 text/html 类型的文件,浏览器就决定直接显示,而一个 .rar 的文件,浏览器就提示用户保存。 直接用一个超链接,链接到一个文件的地址就能下载了 你这个应该是GridView里面的吧,直接将GridView和DataTable绑定,然后写下绑定的表达式就行了。GridView1.DataSource=dt;GridView1.DataBind();前台GridView1中的HypeLink控件....NavigateUrl='<%#Eval("数据库中的对应字段名")%>'..... <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" Text="下载" CommandArgument='<%#Eval("ID")%>' CausesValidation="False" OnCommand="LinkButton1_Command"></asp:LinkButton> </ItemTemplate> protected void LinkButton1_Command(object sender, CommandEventArgs e) { //根据参数来获取文件的路径 string fileID = e.CommandArgument.ToString(); DM dm = new DM(); string strSQL = "select * from 你的表名 where ID=" + int.Parse(fileID); string filePath = dm.getsql(strSQL).Tables[0].Rows[0]["文件URL"].ToString(); ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('" + filePath + "');</script>"); //添加根据文件路径来下载文件 FileStream fileStream = new FileStream(filePath, FileMode.Open); long fileSize = fileStream.Length; Context.Response.ContentType = "application/octet-stream"; //中文文件名需要UTF8编码 string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1); Context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + "\""); Context.Response.AddHeader("Content-Length", fileSize.ToString()); byte[] fileBuffer = new byte[fileSize]; fileStream.Read(fileBuffer, 0, (int)fileSize); fileStream.Close(); Context.Response.BinaryWrite(fileBuffer); Context.Response.End(); } linkbutton调用#region 文件下载 public bool DownLoadFile(string localPath, string hostURL, int byteCount, string userID, long cruuent) { bool result = true; string tmpURL = hostURL; byteCount = byteCount * 1024; hostURL = tmpURL + "&npos=" + cruuent.ToString(); System.IO.FileStream fs; fs = new FileStream(localPath, FileMode.OpenOrCreate); if (cruuent > 0) { //偏移指针 fs.Seek(cruuent, System.IO.SeekOrigin.Current); } System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(hostURL); if (cruuent > 0) { request.AddRange(Convert.ToInt32(cruuent)); //设置Range值 } try { //向服务器请求,获得服务器回应数据流 System.IO.Stream ns = request.GetResponse().GetResponseStream(); byte[] nbytes = new byte[byteCount]; int nReadSize = 0; nReadSize = ns.Read(nbytes, 0, byteCount); while (nReadSize > 0) { fs.Write(nbytes, 0, nReadSize); nReadSize = ns.Read(nbytes, 0, byteCount); } fs.Close(); ns.Close(); } catch(Exception ex) { LOG.Error("下载" + localPath + "的时候失败!" + "原因是:" + ex.Message); fs.Close(); result = false; } return result; } #endregion asp.net 页面过期问题 求助,会的不会的都进来看看。 asp.net在windows运行的话,可以不用IIS吗?用别的比如apache grid启用编辑的时候 能不能加个下拉框 从dropdownlist 里选择 然后再更新呀 怎么实现呢?? 页面加载两个onLoad的问题 关于XslTransform 网上下载的留言板模板怎样和自己的HTML页面结合 简单asp.net页面发布失败 导出到excel,碰到问题 一个挺不解的问题,高手看看啦! 用js判断radiobuttonlist是否被选中并进判断 一个简单的js(jquery)效果
只是获得的文件类型不同,浏览器有不同的行为而已。如一个 text/html 类型的文件,浏览器就决定直接显示,而一个 .rar 的文件,浏览器就提示用户保存。
直接用一个超链接,链接到一个文件的地址就能下载了
GridView1.DataSource=dt;
GridView1.DataBind();前台GridView1中的HypeLink控件
....NavigateUrl='<%#Eval("数据库中的对应字段名")%>'.....
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text="下载" CommandArgument='<%#Eval("ID")%>'
CausesValidation="False" OnCommand="LinkButton1_Command"></asp:LinkButton>
</ItemTemplate> protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
//根据参数来获取文件的路径
string fileID = e.CommandArgument.ToString();
DM dm = new DM();
string strSQL = "select * from 你的表名 where ID=" + int.Parse(fileID);
string filePath = dm.getsql(strSQL).Tables[0].Rows[0]["文件URL"].ToString();
ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('" + filePath + "');</script>");
//添加根据文件路径来下载文件
FileStream fileStream = new FileStream(filePath, FileMode.Open);
long fileSize = fileStream.Length;
Context.Response.ContentType = "application/octet-stream";
//中文文件名需要UTF8编码
string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
Context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + "\"");
Context.Response.AddHeader("Content-Length", fileSize.ToString());
byte[] fileBuffer = new byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
fileStream.Close();
Context.Response.BinaryWrite(fileBuffer);
Context.Response.End();
}
public bool DownLoadFile(string localPath, string hostURL, int byteCount, string userID, long cruuent)
{
bool result = true;
string tmpURL = hostURL;
byteCount = byteCount * 1024;
hostURL = tmpURL + "&npos=" + cruuent.ToString();
System.IO.FileStream fs;
fs = new FileStream(localPath, FileMode.OpenOrCreate);
if (cruuent > 0)
{
//偏移指针
fs.Seek(cruuent, System.IO.SeekOrigin.Current);
}
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(hostURL);
if (cruuent > 0)
{
request.AddRange(Convert.ToInt32(cruuent)); //设置Range值
} try
{
//向服务器请求,获得服务器回应数据流
System.IO.Stream ns = request.GetResponse().GetResponseStream(); byte[] nbytes = new byte[byteCount];
int nReadSize = 0;
nReadSize = ns.Read(nbytes, 0, byteCount);
while (nReadSize > 0)
{
fs.Write(nbytes, 0, nReadSize);
nReadSize = ns.Read(nbytes, 0, byteCount);
}
fs.Close();
ns.Close();
}
catch(Exception ex)
{
LOG.Error("下载" + localPath + "的时候失败!" + "原因是:" + ex.Message);
fs.Close();
result = false;
}
return result;
}
#endregion