上传页面:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form action="UploadStoreByDate.ashx" method="post" enctype="multipart/form-data">
<input type="file" name="file1" />
<input type="submit" value="上传" />
</form>
</body>
</html>
处理页面:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
namespace Web1
{
/// <summary>
/// UploadStoreByDate 的摘要说明
/// </summary>
public class UploadStoreByDate : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
context.Response.Write("<html><head><title>文件保存按日期</title></head><body>");
HttpPostedFile file1 = context.Request.Files["file1"];
if (file1.ContentLength <= 0)
{
context.Response.Write("请选择要上传的文件");
OutputHtmlEnd(context.Response);
return;
}
if (file1.ContentLength > 2 * 1024 * 1024)
{
context.Response.Write("只允许上传不大于1MB的文件");
OutputHtmlEnd(context.Response);
return;
}
string fileExt = Path.GetExtension(file1.FileName);//文件的扩展名
if (fileExt != ".zip" && fileExt != ".rar")
{
context.Response.Write("只允许上传zip、rar....文件");
OutputHtmlEnd(context.Response);
return;
}
string dirPath = DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day;//upload中建的目录按日期创建
string dirFullPath = context.Server.MapPath("~/upload/"+dirPath+"/");//建的目录全路径
string fileFullPath = Path.Combine(dirFullPath,file1.FileName);//一个文件全路径(带文件名)
if (!Directory.Exists(dirFullPath))//如果文件夹不存在,则先创建文件夹
{
Directory.CreateDirectory(dirFullPath);
}
file1.SaveAs(fileFullPath);
context.Response.Write("文件上传成功!");
}
private void OutputHtmlEnd(HttpResponse response)
{
response.Write("</body></html>");
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
SqlHelper类:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
namespace Web1
{
public class SqlHelper
{
//连接字符创
private static readonly string connstr = "Data Source=.;Initial Catalog=Persons;Persist Security Info=True;User ID=sa;Password=123";
//执行查询,返回一张二维表 -----------(查)
public static DataTable ExecuteQuery(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connstr))
using (SqlCommand cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);//连续添加
DataTable dt = new DataTable();//实例化一个表
using(SqlDataReader reader=cmd.ExecuteReader())
{
dt.Load(reader);//查询完一条记录就加载到实例化的表dt中
return dt;
}
}
}
//返回受影响的行数,引用外部的,一个连接,和传进来的参数 ------------(增。删。改)
public static int ExecuteNonQuery(string sql,params SqlParameter[] parameters)
{
using(SqlConnection conn=new SqlConnection(connstr))
using (SqlCommand cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteNonQuery();//返回该语句受影响的行数
}
}
public static object ExecuteScalar(string sql, params SqlParameter[] paramerters)
{
using(SqlConnection conn=new SqlConnection(connstr))
using (SqlCommand cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = sql;
cmd.Parameters.AddRange(paramerters);
return cmd.ExecuteScalar();//返回该语句查找的对象
}
}
}
}