如何从asp.net中的Gridview下载文件? [英] How to download file from Gridview in asp.net?

查看:137
本文介绍了如何从asp.net中的Gridview下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从各个用户上传的gridview链接按钮下载文件但是获得例外







'E:/Latest_jobportal/jvportal/Uploads/purushottam.kumar/purushottam.kumarCV.docx'是物理路径,但预计会有虚拟路径。





我的代码如下





 < span class =code-keyword> if (e.CommandName ==   cmd
{
string filename = e.CommandArgument.ToString();
if (filename!=
{
string path = MapPath(filename);
byte [] bts = System.IO.File.ReadAllBytes(path);
Response.Clear();
Response.ClearHeaders();
Response.AddHeader( Content-Type Application / octet-stream);
Response.AddHeader( Content-Length,bts.Length.ToString()) ;

Response.AddHeader( Content-Disposition attachment; filename = + filename);

Response.BinaryWrite(bts);

Response.Flush();

Response.End();
}
}







}

解决方案

在cs文件中添加以下



  protected   void  DownloadFile( object  sender,EventArgs e)
{
< span class =code-keyword> string filePath =(sender as LinkBut​​ton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader( Content-Disposition attachment; filename = + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
}





在aspx文件中,在Gridview的列中添加Templatefiled



 <   asp:TemplateField     HeaderText   = 下载    HeaderStyle-Width   =  7% >  
< ; ItemTemplate >
< asp:LinkBut​​ton ID = lnkDownload 文字 = 下载 CommandArgument =' <% #Eval( FilePath%> ' runat = server OnClick = DownloadFile > < / asp:LinkBut​​ton >
< / ItemTemplate >
< / asp:TemplateField >


因为你已经有了真正的路径,所以你不需要 MapPath

  byte  [] bts = System.IO.File.ReadAllBytes(filename); 


I want to download file from gridview link button uploaded by respective user but getting exception



'E:/Latest_jobportal/jvportal/Uploads/purushottam.kumar/purushottam.kumarCV.docx' is a physical path, but a virtual path was expected.



my code is as follows


if (e.CommandName == "cmd")
        {
            string filename = e.CommandArgument.ToString();
            if (filename != "")
            {
                string path = MapPath(filename);
                byte[] bts = System.IO.File.ReadAllBytes(path);
                Response.Clear();
                Response.ClearHeaders();
                Response.AddHeader("Content-Type", "Application/octet-stream");
                Response.AddHeader("Content-Length", bts.Length.ToString());

                Response.AddHeader("Content-Disposition", "attachment;   filename=" + filename);

                Response.BinaryWrite(bts);

                Response.Flush();

                Response.End();
            }
}




}

解决方案

In cs file add following

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



In aspx file add Templatefiled in Columns in Gridview

<asp:TemplateField HeaderText="Download" HeaderStyle-Width="7%">
                  <ItemTemplate>
                      <asp:LinkButton ID="lnkDownload" Text="Download" CommandArgument='<%# Eval("FilePath") %>' runat="server" OnClick="DownloadFile"></asp:LinkButton>
                  </ItemTemplate>
              </asp:TemplateField>


since you already has real path, you don't need MapPath

byte[] bts = System.IO.File.ReadAllBytes(filename);


这篇关于如何从asp.net中的Gridview下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆