• .net文件上传


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.IO;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace WebApplication3
    {
    public partial class index : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    string _namepath = "/Files";
    HttpPostedFile files = Request.Files["Upfile"];
    string tpname = Guid.NewGuid().ToString() + new FileInfo(files.FileName).Extension;
    string tpath = filename(Server.MapPath(_namepath));
    string Jpath = tpath + "\\" + tpname;
    string Xpath = _namepath + "/" + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + tpname;
    Upfile.SaveAs(Jpath);
    Response.Write(Xpath);
    }
    //创建文件夹
    public string filename(string Namepath)
    {
    string year = DateTime.Now.Year.ToString();
    string month = "";
    string day = "";
    if (Convert.ToInt32(DateTime.Now.Month.ToString()) > 10)
    {
    month = "0" + DateTime.Now.Month.ToString();
    }
    else
    {
    month = DateTime.Now.Month.ToString();
    }
    if (Convert.ToInt32(DateTime.Now.Day.ToString()) > 10)
    {
    day = "0" + DateTime.Now.Day.ToString();
    }
    else
    {
    day = DateTime.Now.Day.ToString();
    }
    string path = Namepath + @"/" + year + @"/" + month + @"/" + day;
    if (!Directory.Exists(path))
    {
    Directory.CreateDirectory(path);
    }
    return path;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
    //FileUpload1.SaveAs(Server.MapPath(FileUpload1.FileName));
    }
    }
    }

    <asp:FileUpload ID="Upfile" runat="server" />
    &nbsp;<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="上传" />

    <br />
    <br />
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />

  • 相关阅读:
    设计模式的七大原则(Java)
    多线程学习
    力扣题解-1385.两个数组间的距离值
    java集合框架俯瞰
    类加载和双亲委派机制
    关于String
    Object类源码注释
    pom文件标签-仅做手册使用
    effective Java 系列笔记1
    postgres in查询优化
  • 原文地址:https://www.cnblogs.com/qingzhibin/p/upfile.html
Copyright © 2020-2023  润新知