通用上传(到Sql Server)下载文件--完全代码

shang515 2003-12-01 12:34:15
<!--
* * * * * * * * * * * * * * * * * * * * * * * * 天天向上 * * * * * * * * * * * * * * * * * * * * * * * * * * *
* if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[File]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) *
* drop table [dbo].[File]
* GO *
*
* CREATE TABLE [dbo].[File] ( *
* [ID] [int] IDENTITY (1, 1) NOT NULL ,
* [fileName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL , *
* [fileType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
* [fileSize] [float] NULL , *
* [fileContext] [image] NULL
* ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] *
* GO
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-->
<%@ Page language="c#"%>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Data" %>
<script runat="server">
override protected void OnInit(EventArgs e)
{
this.btUp.Click += new System.EventHandler(this.btUp_Click);
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load);
base.OnInit(e);
}
void Page_Load(object sender, System.EventArgs e)
{
this.btUp.Attributes.Add("onclick","loading.style.display='';document.body.enabled=true");
if(Request["downFile"]!=null)//下载文件
{
string strFileID = Request["downFile"];
SqlConnection conn = new SqlConnection("Data Source=localhost;database=test;user id=sa;password=");
SqlDataAdapter adapt = new SqlDataAdapter("select * from [file] where id="+strFileID,conn);
DataSet ds = new DataSet();
adapt.Fill(ds);
if(ds.Tables.Count!=0 && ds.Tables[0].Rows.Count!=0)
{
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename="+ds.Tables[0].Rows[0]["fileName"].ToString());
byte[] context = (Byte[])ds.Tables[0].Rows[0]["fileContext"];
Response.OutputStream.Write(context,0,context.Length);
Response.End();
}
else
{
Response.Write("<font color=red>没有找到要下载的文件</font>");
}
}
else //显示文件列表
{
ListFile();
}
}
void ListFile()
{
SqlConnection conn = new SqlConnection("Data Source=localhost;database=test;user id=sa;password=");
SqlDataAdapter adapt = new SqlDataAdapter("select * from [file]",conn);
DataSet ds = new DataSet();
adapt.Fill(ds);
this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind();
}
void btUp_Click(object sender, System.EventArgs e)
{
if(File.PostedFile!=null && File.PostedFile.ContentLength!=0)
{
int size = File.PostedFile.ContentLength;

SqlConnection conn = new SqlConnection("Data Source=localhost;database=test;user id=sa;password=");
SqlCommand cmd = new SqlCommand("insert into [file] (fileName,fileType,fileSize,fileContext) values(@fileName,@fileType,@fileSize,@fileContext)",conn);
//文件名
SqlParameter param = new SqlParameter("@fileName",SqlDbType.VarChar,50);
param.Value = File.PostedFile.FileName.Substring(File.PostedFile.FileName.LastIndexOf("\\")+1);
cmd.Parameters.Add(param);
//文件类型
param = new SqlParameter("@fileType",SqlDbType.VarChar,50);
param.Value = File.PostedFile.ContentType;
cmd.Parameters.Add(param);
//文件大小
param = new SqlParameter("@fileSize",SqlDbType.Float,8);
param.Value = size;
cmd.Parameters.Add(param);
//文件内容
byte[] context = new Byte[size];
param = new SqlParameter("@fileContext",SqlDbType.Image);
File.PostedFile.InputStream.Read(context,0,size);
param.Value = context;
cmd.Parameters.Add(param);

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

ListFile();
}
else
{
Response.Write("<font color=red>上传文件为空</font>");
}
}
void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=localhost;database=test;user id=sa;password=");
SqlCommand cmd = new SqlCommand("delete [file] where id = @id",conn);
SqlParameter param = new SqlParameter("@id",SqlDbType.Int);
param.Value = e.Item.Cells[0].Text;
cmd.Parameters.Add(param);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

ListFile();
}
</script>
<HTML>
<HEAD>
<title>通用上传下载--支持所有格式文件</title>
</HEAD>
<body>
<form id="WebForm5" method="post" runat="server" enctype="multipart/form-data">
<input id="File" type="file" runat="server">
<asp:button id="btUp" runat="server" Text="上 传"></asp:button><asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
<HeaderStyle BackColor="#669966"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="ID">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="fileName" HeaderText="文件名">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="fileType" HeaderText="文件类型">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="fileSize" HeaderText="文件大小">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
</asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:HyperLink Text="下载" NavigateUrl='<%# String.Format("{0}?downFile={1}",Request.CurrentExecutionFilePath,DataBinder.Eval(Container.DataItem,"ID"))%>' Runat="server">
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid></form>
<div id="loading" style="display:none;border-left:1px solid #EEEEEE;border-top:1px solid #EEEEEE;border-right:1px solid #666666;border-bottom:1px solid #666666;background-color:#DDDDDD;width:300px;heigth:200px;text-align:center">
正在进行操作,请稍候……
</div>
</body>
</HTML>
...全文
214 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
gucs 2003-12-24
  • 打赏
  • 举报
回复
改天再看
xufengo0oID 2003-12-24
  • 打赏
  • 举报
回复
正需要呢!谢了!
jxf_yx 2003-12-14
  • 打赏
  • 举报
回复
up
hanfey 2003-12-14
  • 打赏
  • 举报
回复
mark!
happstar 2003-12-13
  • 打赏
  • 举报
回复
不错嘛,混到三个星了。向大哥学习一下。呵呵。。。
monimm 2003-12-13
  • 打赏
  • 举报
回复
up
xueqs 2003-12-13
  • 打赏
  • 举报
回复
mark
wugw 2003-12-13
  • 打赏
  • 举报
回复
我们以前用ADO的STREAM做上传传过10多M的文件,我尝试传。NET SDK (NWGS)100多M时没有耐心我中途退出了。我建议你用这个COM组件试试吧,在。NET中会更简单,创建OLE的方法VS。NET中就有例子,我想你会吧,其它就不用解释了,实在不会你搜索一下STREAM的属性与方法就可以,区区0。5M不在话在的。
Wuzhou98 2003-12-13
  • 打赏
  • 举报
回复
对于大文件,就要用COM组件了
极限999 2003-12-13
  • 打赏
  • 举报
回复
up
tembo 2003-12-13
  • 打赏
  • 举报
回复
有没有vb版的??
163lzm 2003-12-03
  • 打赏
  • 举报
回复
mark
wanghuixue 2003-12-02
  • 打赏
  • 举报
回复
up
jackyoung02 2003-12-02
  • 打赏
  • 举报
回复
收了
男人看胆 2003-12-02
  • 打赏
  • 举报
回复
applause
bpy 2003-12-02
  • 打赏
  • 举报
回复
gz
xiaocaonet 2003-12-02
  • 打赏
  • 举报
回复
好东西,谢谢!
mcwj 2003-12-02
  • 打赏
  • 举报
回复
up
Wuzhou98 2003-12-02
  • 打赏
  • 举报
回复
thanks, mark
wjinwei 2003-12-02
  • 打赏
  • 举报
回复
谢谢!收藏
加载更多回复(17)

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧