下载word文档

来源:http://www.cnblogs.com/damonlan/archive/2012/04/28/2473525.html

作者:浪迹天涯

复制代码
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string courseName = ((Label)GridView1.Rows[e.RowIndex].Cells[1].FindControl("Label1")).Text.ToString();//在GridView中文件名字
        string time = ((Label)GridView1.Rows[e.RowIndex].Cells[2].FindControl("Label2")).Text.ToString();//在GridView中找时间
        string tempPath = BusyworkManage.Path + tm.ReturnTeacherID(Request.Cookies["StudentID"].Value.ToString()) +BusyworkManage.TopicPath + 
courseName + "/" + courseName + "_" + time + ".doc";//这样做是为了不使下载后的文件的名字重复!~~。
        string path = Server.MapPath(tempPath);
        FileInfo fInfo = new FileInfo(path);
        string fname = fInfo.Name;
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fname));
        Response.AddHeader("Content-Length", fInfo.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(fInfo.FullName);
        Response.Flush();
复制代码

首先需要说明的是,在gridview控件中放一个button 按钮,如下:

复制代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="139px"
        Width="100%" OnRowDeleting="GridView1_RowDeleting" 
            onselectedindexchanged="GridView1_SelectedIndexChanged">
        <Columns>
        。。。。。。省略代码。。。。。
            <asp:BoundField DataField="成绩" HeaderText="成绩">
                <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:CommandField ButtonType="Button" DeleteText="下作业载" ShowDeleteButton="True">
                <ItemStyle HorizontalAlign="Center" />
            </asp:CommandField>
            <asp:HyperLinkField Text="提交作业" DataNavigateUrlFormatString="SubmitBusywork.aspx?course={0}&amp;time={1}" DataNavigateUrlFields="课程名称,作业次数">
                <ItemStyle HorizontalAlign="Center" />
            </asp:HyperLinkField>
        </Columns>
    </asp:GridView>
复制代码

 

作者:Lanny☆兰东才
出处:http://www.cnblogs.com/damonlan
Q Q:*********
E_mail:Damon_lan@163.com or Dongcai.lan@hp.com

本博文欢迎大家浏览和转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,在『参考』的文章中,我会表明参考的文章来源,尊重他人版权。若您发现我侵犯了您的版权,请及时与我联系。

 

posted on   华山青竹  阅读(305)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示