日期:2014-05-16  浏览次数:20545 次

C#从数据库中上传下载附件功能

Page代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo1.aspx.cs" Inherits="order.Demo.Demo1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Demo1</title>
    <script type="text/javascript" src="../js/jquery-1.6.2.min.js"></script>
    <script type="text/javascript" src="../js/Global.js"></script>
    <script type="text/javascript" src="../js/Calendar.js"></script>
    <link type="text/css" rel="Stylesheet" href="../css/Default.css" />
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
        CellPadding="3" GridLines="Horizontal" Width="100%" 
        onrowdatabound="GridView1_RowDataBound" 
        onrowcommand="GridView1_RowCommand" CssClass="text">
        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
        <Columns>
            <asp:TemplateField HeaderText="选择">
                <ItemTemplate>
                    <input ID="RadioLine" name="RadioLine" type="radio" value="<%#Eval("accessory_id")%>" />
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:BoundField DataField="accessory_id" HeaderText="附件ID" />
            <asp:BoundField DataField="order_header_id" HeaderText="订单ID" />
            <asp:BoundField DataField="accessory_name" HeaderText="附件名称" />
            <asp:BoundField DataField="accessory_description" HeaderText="附件说明" />
            <asp:BoundField DataField="creation_date" DataFormatString="{0:d}" HeaderText="创建日期" />
            <asp:BoundField DataField="created_by" HeaderText="创建人" />
            <asp:BoundField DataField="accessory_size" HeaderText="文件大小" />
            <asp:BoundField DataField="accessory_type" HeaderText="文件类型" />
            <asp:ButtonField CommandName="DownItem" HeaderText="下载" ShowHeader="True" Text="下载" >
            <ItemStyle HorizontalAlign="Center" />
            </asp:ButtonField>
            <asp:ButtonField CommandName="DelItem" HeaderText="删除" ShowHeader="True" Text="删除" >
            <ItemStyle HorizontalAlign="Center" />
            </asp:ButtonField>
        </Columns>
        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
        <AlternatingRowStyle BackColor="#F7F7F7" />
    </asp:GridView>
    <asp:FileUpload ID="FileUpload1" runat="server" ToolTip="附件上传" />
    <asp:Button ID="BtnUpload" runat="server" Text="上传" OnClick="BtnUpload_Files"  />
    <input type="hidden" id="hidden_id" runat=server/> 
    </form>
</body>
</html>

<script language="javascript" type="text/javascript">
    // 注销
    function fn_DelItem(cellstring) {
        if (!confirm("确定要删除附件吗?")) {
            return false;
        }
        return true;
    }

    // 注销
    function fn_DownItem(cellstring) {
        return true;
    }

</script>

?C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.IO;
using System.Data.OracleClient;
using order.Com