我有一个gridview,其中我有一个按钮字段。这个按钮字段的功能是允许我下载保存在我的数据库中的文档?但是当我点击下载按钮时没有任何反应? [英] i have a gridview in which i have one button field. the functionality of this button field is to allow me to download docs saved in my database?but nothing happens when i hit the download button?
本文介绍了我有一个gridview,其中我有一个按钮字段。这个按钮字段的功能是允许我下载保存在我的数据库中的文档?但是当我点击下载按钮时没有任何反应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据库包含以下数据:
autoid int
pcode varchar ( 50 )
fyyear varchar (< span class =code-digit> 50 )
date varchar ( 50 )
薪水 int
ta int
contigency int
nrc int
institcharges int
others int
docname varchar ( 50 )
doctype varchar ( 50 )
docdata varbinary (MAX)
这是我用来在按钮字段中显示数据的代码:
protected void GridView1_RowCommand( object sender,GridViewCommandEventArgs e)
{
if (e.CommandName == 文件下载)
{
int index = Convert.ToInt32(e.CommandArgument);
string url = GridView1.Rows [index] .Cells [ 13 ]。文字;
System.IO.FileInfo file = new System.IO.FileInfo(url);
if (file.Exists)
{
Response.Clear();
Response.AppendHeader( Content-Disposition:, attachment; filename = + file.Name);
Response.AppendHeader( Content-Length,file.Length.ToString()) ;
Response.ContentType = application / octet-stream;
Response.TransmitFile(file.FullName);
Response.End();
}
else
{
Response.Write( NO FILE PRESENT);
}
}
}
但是当我点击按钮字段时没有任何反应
注意:我将我的按钮字段链接到docname数据字段。
解决方案
有两种方法:
1st:
int index = Convert.ToInt32(e.CommandArgument);
string url = GridView1.Rows [index] .Cells [ 13 ]。文字;
Response.Redirect(url)
和
第二名:
int index = Convert.ToInt32(e.CommandArgument);
string url = GridView1.Rows [index] .Cells [ 13 ]。文字;
FileInfo file = new FileInfo(url);
if (file.Exists)
{
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader( content-disposition, attachment; filename = + fileName);
Response.ContentType = application / octet-stream;
Response.AddHeader( Content-Length,file.Length.ToString()) ;
Response.WriteFile(file.FullName);
Response.End();
}
else
{
Response.Write( 此文件不存在。);
}
希望它有效!
--Amit
Response.TransmitFile(Server.MapPath(~C / Desktop / MIS / docs /+ fileName));
im在这里收到错误无法找到路径的一部分 C:\Users\cgrt\Desktop\MIS\〜C\Desktop\MIS\docs\ ''
。
my database contains following feilds:
autoid int
pcode varchar(50)
fyyear varchar(50)
date varchar(50)
salary int
ta int
contigency int
nrc int
institcharges int
others int
docname varchar(50)
doctype varchar(50)
docdata varbinary(MAX)
This is the code i am using to display the data in the button field:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Document Download")
{
int index = Convert.ToInt32(e.CommandArgument);
string url = GridView1.Rows[index].Cells[13].Text;
System.IO.FileInfo file = new System.IO.FileInfo(url);
if (file.Exists)
{
Response.Clear();
Response.AppendHeader("Content-Disposition:", "attachment; filename=" + file.Name);
Response.AppendHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.TransmitFile(file.FullName);
Response.End();
}
else
{
Response.Write("NO FILE PRESENT");
}
}
}
but when i click the button field nothing happens
note: i have linked my button field to docname data field.
解决方案
There is two ways:
1st:
int index = Convert.ToInt32(e.CommandArgument); string url = GridView1.Rows[index].Cells[13].Text; Response.Redirect(url)
And
2nd:
int index = Convert.ToInt32(e.CommandArgument); string url = GridView1.Rows[index].Cells[13].Text; FileInfo file = new FileInfo(url); if (file.Exists) { Response.Clear(); Response.ClearHeaders(); Response.ClearContent(); Response.AddHeader("content-disposition", "attachment; filename=" + fileName); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Length", file.Length.ToString()); Response.WriteFile(file.FullName); Response.End(); } else { Response.Write("This file does not exist."); }
Hope it works!
--Amit
Response.TransmitFile(Server.MapPath("~C/Desktop/MIS/docs/" + fileName));
i m getting error here "Could not find a part of the path ''C:\Users\cgrt\Desktop\MIS\~C\Desktop\MIS\docs\''."
这篇关于我有一个gridview,其中我有一个按钮字段。这个按钮字段的功能是允许我下载保存在我的数据库中的文档?但是当我点击下载按钮时没有任何反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文