html

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUploadDataSource.aspx.cs" Inherits="WebApplication1.FileUploadDataSource" %>

<!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></title>
<style type ="text/css" >
.fileList li{ margin-bottom :5px;}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="upFile" runat="server" />
<asp:Button ID="Button1" runat="server"
Text="上传" onclick="Button1_Click" />
</div>
<div>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="Sqldatasource1">
<HeaderTemplate >
<ul class ="fileList">
</HeaderTemplate>
<ItemTemplate >
<li>
<asp:HyperLink ID="HyperLink1" runat="server" Text ='<%#Eval("FileName")%>' NavigateUrl ='<%#Eval("Id","~/FileHandler.ashx?id={0}") %>'>HyperLink</asp:HyperLink>
</li>
</ItemTemplate>
<FooterTemplate >
</ul>
</FooterTemplate>
</asp:Repeater>
</div>
<asp:sqldatasource ID="Sqldatasource1" runat="server"
ConnectionString="Data Source=localhost;Initial Catalog=test;Integrated Security=True"
ProviderName="System.Data.SqlClient" InsertCommand="insert into Files(FileName,FileBytes)values(@FileName,@FileBytes)"
SelectCommand="SELECT Files.* FROM Files">
<InsertParameters>
<asp:ControlParameter ControlID="upFile" Name="FileName"
PropertyName="FileName" />
<asp:ControlParameter ControlID ="upFile" Name ="FileBytes" PropertyName ="FileBytes" />
</InsertParameters>
</asp:sqldatasource>
</form>
</body>
</html>

后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO; namespace WebApplication1
{
public partial class FileUploadDataSource : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void Button1_Click(object sender, EventArgs e)
{
if (upFile.HasFile)
{
if (CheckFileType(upFile.FileName))
Sqldatasource1.Insert();
}
} private bool CheckFileType(string fileName)
{
return (Path.GetExtension(fileName).ToLower() == ".doc" || Path.GetExtension(fileName).ToLower() == ".docx");
}
}
}

FileHandler处理页面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data; namespace WebApplication1
{
/// <summary>
/// FileHandler 的摘要说明
/// </summary>
public class FileHandler : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/msword";
string conString = "Data Source=localhost;Initial Catalog=test;Integrated Security=True";
using (SqlConnection conn = new SqlConnection(conString))
{
string sql = "SELECT FileName, FileBytes FROM Files where Id=@id \n";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
conn.Open();
cmd.Parameters.AddWithValue("@Id", context.Request["Id"]);
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
adapter.Fill(dt);
if (dt.Rows.Count > )
{
byte[] fileBytes = (byte[])dt.Rows[]["FileBytes"];
string fileName = dt.Rows[]["FileName"].ToString();
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
context.Response.BinaryWrite(fileBytes);
}
else
{
context.Response.ContentType = "text/plain";
context.Response.Write("文件不存在");
}
}
}
} } public bool IsReusable
{
get
{
return false;
}
}
}
}

CREATE TABLE [dbo].[Files](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FileName] [nvarchar](50) NULL,
[FileBytes] [varbinary](max) NULL,
CONSTRAINT [PK_Files] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

最新文章

  1. Socket.io和Redis写Realtime App 之express初试
  2. jmeter执行顺序
  3. iOS蓝牙开发(一)蓝牙相关基础知识(转)
  4. 正确配置Linux系统ulimit值的方法【转】
  5. 轻松学习Linux之自动执行任务
  6. MAT(1) 小样
  7. 电容值E系列标称方法
  8. c#后台验证
  9. ViewPager 详解(三)---PagerTabStrip与PagerTitleStrip添加标题栏的异同
  10. Django搭建网站笔记
  11. django之数据库表的单表查询
  12. 秒懂HTTPS
  13. python基础--列表、元祖、字典、集合
  14. python-web自动化-Python+Selenium之expected_conditions:各种判断
  15. 冲刺Two之站立会议10
  16. Hive 的排名和跨行 窗口函数及其使用
  17. BZOJ.4695.最假女选手(线段树 Segment tree Beats!)
  18. nginx location正则写法
  19. 使用spring的aop监听所有controller或者action日志
  20. 使用spring中的@Transactional注解时,可能需要注意的地方

热门文章

  1. Java Web 应用概述
  2. Git 删除所有历史提交记录方法
  3. 【转帖】LSM树 和 TSM存储引擎 简介
  4. 【转帖】极简Docker和Kubernetes发展史
  5. 前端中常见字节编码(base64、hex、utf8)及其转换
  6. Java-手动搭建SSH(maven版)
  7. springboot+mybatis实现数据库的读写分离
  8. CentOS7安装rabbitmq集群(二进制)
  9. Wampserver图标黄色解决
  10. SpringBoot使用mybatis,发生:Failed to configure a DataSource: &#39;url&#39; attribute is not specified and no embedded datasource could be configured