• .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" />

  • 相关阅读:
    RecyclerView,ListView first item get focus
    一种粗暴快速的Android全屏幕适配方案(转)
    谈谈单元测试之(一):为什么要进行烦人的单元测试(转)
    SCIgen与野鸡期刊的梗
    百度搜索仍然是【最快解决工作问题的方法】
    搭建minima主题的github博客网站
    本科期间的第一个专利:改卷筒的故事
    学习消费思维的【贫穷与富有】
    我的联系方式
    学习 GitHub 有什么好处?
  • 原文地址:https://www.cnblogs.com/qingzhibin/p/upfile.html
Copyright © 2020-2023  润新知