做了个上传下载任意文件到数据库
效果如下
效果如上
上这代码大多不是我写的,只是改了 cnblogs 一位网友的...
http://singlepine.cnblogs.com/articles/288027.html
他是输出一个图片,我是要下载这个文件
效果如上
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.IO;
using System.Net ;
namespace WebApplication2
{
/// <summary>
/// Summary description for ImageGrid.
/// </summary>
public class ImageGrid : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.WebControls.Button btnsave;
protected System.Web.UI.WebControls.TextBox txtUserName;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Button Del;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
Data_Bind();
}
}
GetDataSet
DataBind
FormatImage
Web Form Designer generated code
btnsave_Click
private void Del_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem item in this.DataGrid1.Items)
{
CheckBox cb=(CheckBox)item.FindControl("select");
if (cb.Checked)
{
this.del( int.Parse (this.DataGrid1.DataKeys[item.ItemIndex].ToString ()));
}
}
Data_Bind();
}
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1 .CurrentPageIndex =e.NewPageIndex ;
Data_Bind();
}
获取本机ip
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.IO;
using System.Net ;
namespace WebApplication2
{
/// <summary>
/// Summary description for ImageGrid.
/// </summary>
public class ImageGrid : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.WebControls.Button btnsave;
protected System.Web.UI.WebControls.TextBox txtUserName;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Button Del;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
Data_Bind();
}
}
GetDataSet
DataBind
FormatImage
Web Form Designer generated code
btnsave_Click
private void Del_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem item in this.DataGrid1.Items)
{
CheckBox cb=(CheckBox)item.FindControl("select");
if (cb.Checked)
{
this.del( int.Parse (this.DataGrid1.DataKeys[item.ItemIndex].ToString ()));
}
}
Data_Bind();
}
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1 .CurrentPageIndex =e.NewPageIndex ;
Data_Bind();
}
获取本机ip
}
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb ;
namespace WebApplication2
{
/// <summary>
/// Summary description for ReadImage.
/// </summary>
public class ReadImage : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
int UserID = Convert.ToInt32(Request["ID"]);
string sql="select * from file where ID="+UserID;
DataSet ds = GetDataSet(sql);
if (ds != null && ds.Tables[0].Rows.Count>0)
{
Response.Clear();
string Type=ds.Tables[0].Rows[0]["Type"].ToString();
string name=ds.Tables[0].Rows[0]["name"].ToString ();
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.ContentType=Type;
string realtype=ds.Tables[0].Rows[0]["realtype"].ToString ();
name=Server.UrlEncode(name);
name=Server.UrlDecode(name);
Response.AddHeader("Content-Disposition", "attachment; filename="+System.Web.HttpUtility.UrlEncode(name)+""+realtype);
Response.BinaryWrite((byte[])ds.Tables[0].Rows[0]["filevalue"]);
Response.End();
}
}
GetDataSet
Web Form Designer generated code
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb ;
namespace WebApplication2
{
/// <summary>
/// Summary description for ReadImage.
/// </summary>
public class ReadImage : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
int UserID = Convert.ToInt32(Request["ID"]);
string sql="select * from file where ID="+UserID;
DataSet ds = GetDataSet(sql);
if (ds != null && ds.Tables[0].Rows.Count>0)
{
Response.Clear();
string Type=ds.Tables[0].Rows[0]["Type"].ToString();
string name=ds.Tables[0].Rows[0]["name"].ToString ();
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.ContentType=Type;
string realtype=ds.Tables[0].Rows[0]["realtype"].ToString ();
name=Server.UrlEncode(name);
name=Server.UrlDecode(name);
Response.AddHeader("Content-Disposition", "attachment; filename="+System.Web.HttpUtility.UrlEncode(name)+""+realtype);
Response.BinaryWrite((byte[])ds.Tables[0].Rows[0]["filevalue"]);
Response.End();
}
}
GetDataSet
Web Form Designer generated code
}
}
上这代码大多不是我写的,只是改了 cnblogs 一位网友的...
http://singlepine.cnblogs.com/articles/288027.html
他是输出一个图片,我是要下载这个文件