zoukankan      html  css  js  c++  java
  • 练习文件保存按照日期来创建文件夹,并保存

    上传页面:
    <!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();//返回该语句查找的对象
    }
    }

    }
    }

  • 相关阅读:
    sql语句左链接left join--3张表关联
    combobox下拉框
    sql in()批量操作
    spring事务传播特性实验(2):PROPAGATION_REQUIRED实验结果与分析
    【kubernetes】kubectl logs connection refused
    k8s1.4.3安装实践记录(3)下载基础镜像
    k8s1.4.3安装实践记录(2)-k8s安装
    k8s1.4.3安装实践记录(1)-etcd、docker、flannel安装配置
    python爬虫常用数据整理函数
    django-xadmin常用内容记录
  • 原文地址:https://www.cnblogs.com/DoNetCShap/p/9776581.html
Copyright © 2011-2022 走看看