做了个上传下载任意文件到数据库

  效果如下
 
temp.GIF


效果如上




None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data.OleDb;
None.gif
using  System.IO;
None.gif
using  System.Net ;
None.gif
namespace  WebApplication2
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for ImageGrid.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class ImageGrid : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
InBlock.gif        
protected System.Web.UI.WebControls.Button btnsave;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox txtUserName;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label3;
InBlock.gif        
protected System.Web.UI.WebControls.Button Del;
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
InBlock.gif        
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{   
InBlock.gif            
if (!this.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Data_Bind();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
GetDataSet#region GetDataSet
InBlock.gif        
private DataSet GetDataSet(string sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string constring=System.Configuration.ConfigurationSettings.AppSettings[0];
InBlock.gif            OleDbDataAdapter    sda 
=new OleDbDataAdapter(sql,constring);
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            sda.Fill(ds);
InBlock.gif            
return ds;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
DataBind#region DataBind
InBlock.gif        
private void Data_Bind()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{   string sql="";
InBlock.gif            
if (getIPAddress ()=="10.34.76.100")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sql
="select * from [file] order by id desc ";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sql
="select * from [file] where ip='"+getIPAddress ()+"'  order by id desc ";
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif                
InBlock.gif        DataSet ds
=GetDataSet(sql);
InBlock.gif            
this.DataGrid1.DataSource=ds;
InBlock.gif            
this.DataGrid1.DataBind();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
dot.gif#region
InBlock.gif        
public  void  del(int id)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string sql="delete * from [file] where id="+id; 
InBlock.gif     System.Data .OleDb .OleDbConnection cnn
=new System.Data.OleDb.OleDbConnection (System.Configuration.ConfigurationSettings.AppSettings[0]);
InBlock.gif            System.Data .OleDb .OleDbCommand odc
=new OleDbCommand (sql,cnn);
InBlock.gif            cnn.Open ();
InBlock.gif            odc.ExecuteNonQuery ();
InBlock.gif            cnn.Close();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
FormatImage#region FormatImage
InBlock.gif        
protected string FormatImage(object obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return "ReadImage.aspx?UserID=" + obj.ToString();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web Form Designer generated code#region Web Form Designer generated code
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.btnsave.Click += new System.EventHandler(this.btnsave_Click);
InBlock.gif            
this.Del.Click += new System.EventHandler(this.Del_Click);
InBlock.gif            
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
btnsave_Click#region btnsave_Click
InBlock.gif        
private void btnsave_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
InBlock.gif            
string Path=File1.PostedFile.FileName;// 文件名称
InBlock.gif
            int Size = File1.PostedFile.ContentLength; // 文件大小
InBlock.gif
            string Type = File1.PostedFile.ContentType;// 文件类型
InBlock.gif
            Stream ImageStream = File1.PostedFile.InputStream;
InBlock.gif            
byte[] Content = new byte[Size];
InBlock.gif            
int Status = ImageStream.Read(Content, 0, Size);
InBlock.gif
InBlock.gif            
// 写入数据库
InBlock.gif
             System.Data .OleDb .OleDbConnection cnn=new System.Data.OleDb.OleDbConnection (System.Configuration.ConfigurationSettings.AppSettings[0]);
InBlock.gif
InBlock.gif            System.Data .OleDb .OleDbCommand comm
=new OleDbCommand("insert into [file]([name],filevalue,uptime,Type,realtype,ip) values(@UserName,@Image,@Path,@Type,@realtype,@ip)",cnn);
InBlock.gif
InBlock.gif            comm.CommandType 
= CommandType.Text;
InBlock.gif            comm.Parameters.Add(
"@name", OleDbType.VarChar, 255).Value = txtUserName.Text;            
InBlock.gif            comm.Parameters.Add(
"@filevalue",OleDbType.Binary ).Value = Content;
InBlock.gif            comm.Parameters.Add(
"@uptime", OleDbType.VarChar,50).Value = System.DateTime.Now.Date.ToString ("yyyy-MM-dd");
InBlock.gif            comm.Parameters.Add(
"@Type", OleDbType.VarChar, 50).Value = Type;
InBlock.gif        
InBlock.gif            
string abc=@File1.PostedFile .FileName ;
InBlock.gif            
string temp=abc.Substring (abc.IndexOf ("."));
InBlock.gif
InBlock.gif            comm.Parameters .Add (
"@realtype",OleDbType.VarChar,50).Value=temp;
InBlock.gif             comm.Parameters .Add (
"@ip",OleDbType.VarChar ,50).Value=getIPAddress ();
InBlock.gif            cnn.Open ();
InBlock.gif            comm.ExecuteNonQuery();
InBlock.gif            cnn.Close();
InBlock.gif            Data_Bind();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void Del_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
foreach(DataGridItem item in this.DataGrid1.Items)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                CheckBox cb
=(CheckBox)item.FindControl("select");
InBlock.gif                
if (cb.Checked)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                   
this.del( int.Parse (this.DataGrid1.DataKeys[item.ItemIndex].ToString ()));
InBlock.gif
InBlock.gif                    
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif        Data_Bind();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
this.DataGrid1 .CurrentPageIndex =e.NewPageIndex ;
InBlock.gif        Data_Bind();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
获取本机ip#region  获取本机ip
InBlock.gif        
private static string getIPAddress ( )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Net.IPAddress addr;
InBlock.gif            
// 获得本机局域网IP地址
InBlock.gif
            addr = new System.Net.IPAddress ( Dns.GetHostByName (Dns.GetHostName ( ) ).AddressList [0].Address) ;
InBlock.gif            
return addr.ToString ( ) ;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif







None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data.OleDb ;
None.gif
namespace  WebApplication2
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for ReadImage.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class ReadImage : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int UserID = Convert.ToInt32(Request["ID"]);
InBlock.gif            
string sql="select * from file where ID="+UserID;
InBlock.gif            DataSet ds 
= GetDataSet(sql);
InBlock.gif
InBlock.gif            
if (ds != null && ds.Tables[0].Rows.Count>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Response.Clear();
InBlock.gif                
InBlock.gif                
string Type=ds.Tables[0].Rows[0]["Type"].ToString();
InBlock.gif                
string name=ds.Tables[0].Rows[0]["name"].ToString ();
InBlock.gif
InBlock.gif                 Response.ContentEncoding 
= System.Text.Encoding.GetEncoding("gb2312"); 
InBlock.gif                 Response.ContentType
=Type;
InBlock.gif                
string realtype=ds.Tables[0].Rows[0]["realtype"].ToString ();
InBlock.gif
InBlock.gif            
InBlock.gif                
InBlock.gif                 name
=Server.UrlEncode(name);
InBlock.gif                 name
=Server.UrlDecode(name);
InBlock.gif                Response.AddHeader(
"Content-Disposition""attachment; filename="+System.Web.HttpUtility.UrlEncode(name)+""+realtype); 
InBlock.gif    
InBlock.gif                Response.BinaryWrite((
byte[])ds.Tables[0].Rows[0]["filevalue"]);
InBlock.gif                Response.End();
InBlock.gif                
InBlock.gif               
InBlock.gif              
InBlock.gif             
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif            }
    
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
GetDataSet#region GetDataSet
InBlock.gif        
private DataSet GetDataSet(string sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string constring=System.Configuration.ConfigurationSettings.AppSettings[0];
InBlock.gif            OleDbDataAdapter    sda 
=new OleDbDataAdapter(sql,constring);
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            sda.Fill(ds);
InBlock.gif            
return ds;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web Form Designer generated code#region Web Form Designer generated code
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

上这代码大多不是我写的,只是改了 cnblogs 一位网友的...

http://singlepine.cnblogs.com/articles/288027.html

他是输出一个图片,我是要下载这个文件


 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值