上传文件到数据库没任何问题
我的数据表是myfile
如下:
Id int 
files image (存放数据文件)
title  varchar (文件名不带扩展名)
Type   varchar(文件类型(看是doc,txt等))
FileDx  varchar(文件大小)
我的前台页面为
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="download.aspx.cs" Inherits="download" %><!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="371px"  _disibledevent="GridView1_RowCommand" OnRowCommand="GridView1_RowCommand">
                <Columns>
                    <asp:BoundField  DataField="Id" HeaderText="编号" />
                    <asp:BoundField DataField="type" HeaderText="文件类型" />
                    <asp:BoundField DataField="title" HeaderText="文件名称" />
                    <asp:TemplateField HeaderText="下载">
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CommandName="dnl" CommandArgument="Id"><%# DataBinder.Eval(Container.DataItem, "Id")%></asp:LinkButton>
                       <a href="DownLoad.aspx?Id=<%# DataBinder.Eval(Container.DataItem,"Id") %>" target="_blank">DownLoad</a>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>   
    </div>
    </form>
</body>
后台程序为
(就是想实现选中GridView1中的某一行就可以在在本页直接下载)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;public partial class download : System.Web.UI.Page
{
    int i;
    protected void Page_Load(object sender, EventArgs e)
    {        //下载页面
        //int i = Convert.ToInt32(Request.QueryString["Id"]);
        //绑定GridView1
        if (!IsPostBack)
        {
            SqlConnection SqlCon1 = new SqlConnection("server=local;database=acount;User Id=HI;Password=HI");
            string SqlStr = "select * from myfile";
            SqlDataAdapter SqlSda = new SqlDataAdapter(SqlStr, SqlCon1);
            DataSet Myds = new DataSet();
            SqlCon1.Open(); 
            SqlSda.Fill(Myds, "myfile");
            GridView1.DataSource = Myds;
            GridView1.DataBind();
        }
            SqlConnection SqlCon = new SqlConnection("server=local;database=acount;User Id=HI;Password=HI");
            SqlCon.Open();
            //int i = Convert.ToInt32(ww.ToString());
            SqlDataAdapter Sda = new SqlDataAdapter("select   *   from   myfile   where   Id=  " + i, SqlCon);
            DataSet Ds = new DataSet();
            Sda.Fill(Ds, "hastable");
            byte[] b2 = (byte[])Ds.Tables["hastable"].Rows[0]["files"];
            string type = (string)Ds.Tables["hastable"].Rows[0]["Type"];
            Response.Clear();
            string Stype = checktype(type);
            Response.AddHeader("Content-Disposition", "attachment;   filename=abc" + type);
            Response.AddHeader("Content-Length", b2.Length.ToString());
            Response.ContentType = Stype;
            Response.BinaryWrite(b2);
            Response.End();
            string FileName = ((LinkButton)sender).CommandArgument;
            Response.Clear();
            Response.ContentType = Stype;
            Response.AddHeader("Content-Disposition", "attachment;FileName=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
            Response.WriteFile(FileName);
            Response.End();
    }
    private string checktype(string filename)
    { 
     //文件类型判断
        string ContentType;
        switch (filename.ToLower())
        {
         case ".asf":
         ContentType= "video/x-ms-asf";
         break;
        case ".avi":
        ContentType = "video/avi";
        break;
        case ".doc":
        ContentType = "application/msword";
        break;
        case ".zip":
        ContentType = "application/zip";
        break;
        case ".xls":
        ContentType = "application/vnd.ms-excel";
        break;
        case ".gif":
        ContentType = "image/gif";
        break;
        case ".jpg":
        ContentType = "image/jpeg";
        break;
        case "jpeg":
        ContentType = "image/jpeg";
        break;
        case ".wav":
        ContentType = "audio/wav";
        break;
        case ".mp3":
        ContentType = "audio/mpeg3";
        break;
        case ".mpg":
        ContentType = "video/mpeg";
        break;
        case ".mepg":
        ContentType = "video/mpeg";
        break;
        case ".rtf":
        ContentType = "application/rtf";
        break;
        case ".html":
        ContentType = "text/html";
        break;
        case ".htm":
        ContentType = "text/html";
        break;
        case ".txt":
        ContentType = "text/plain";
        break;
        default:
        ContentType = "application/octet-stream";
        break;
        }
        return ContentType;
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {       
        i = Convert.ToInt32(e.CommandArgument);
    }}
一运行总出错"i的值为0"
请大家指教一下,多谢!

解决方案 »

  1.   

     int   i; 存入viewstate不要让它成为一般类变量,因为b/s程序是无状态的
    另外最好新建一页下载
      

  2.   

    我感觉是i赋值的问题,因为pageload事件会先于rowcommand执行,把pageload中的事件放入rowcommand中i赋值后执行试试
      

  3.   

     protected   void   GridView1_RowCommand(object   sender,   GridViewCommandEventArgs   e) 
            {               
                    i   =   Convert.ToInt32(e.CommandArgument); 
     SqlConnection   SqlCon   =   new   SqlConnection("server=local;database=acount;User   Id=HI;Password=HI"); 
                            SqlCon.Open(); 
                            //int   i   =   Convert.ToInt32(ww.ToString()); 
                            SqlDataAdapter   Sda   =   new   SqlDataAdapter("select       *       from       myfile       where       Id=     "   +   i,   SqlCon); 
                            DataSet   Ds   =   new   DataSet(); 
                            Sda.Fill(Ds,   "hastable"); 
                            byte[]   b2   =   (byte[])Ds.Tables["hastable"].Rows[0]["files"]; 
                            string   type   =   (string)Ds.Tables["hastable"].Rows[0]["Type"]; 
                            Response.Clear(); 
                            string   Stype   =   checktype(type); 
                            Response.AddHeader("Content-Disposition",   "attachment;       filename=abc"   +   type); 
                            Response.AddHeader("Content-Length",   b2.Length.ToString()); 
                            Response.ContentType   =   Stype; 
                            Response.BinaryWrite(b2); 
                            Response.End(); 
                            string   FileName   =   ((LinkButton)sender).CommandArgument; 
                            Response.Clear(); 
                            Response.ContentType   =   Stype; 
                            Response.AddHeader("Content-Disposition",   "attachment;FileName="   +   HttpUtility.UrlEncode(FileName,   System.Text.Encoding.UTF8)); 
                            Response.WriteFile(FileName); 
                            Response.End();         } 
      

  4.   

    我按照4楼的老兄说的做了,但是点下载后这一句报错“i       =       Convert.ToInt32(e.CommandArgument); ”说输入的字符串格式不正确,请大家指点一下,急....!