zoukankan      html  css  js  c++  java
  • .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" />

  • 相关阅读:
    cookie
    sql 语句
    页面宽高
    分页
    asp.net中如何防止用户重复点击提交按钮
    小试简单工厂模式之简单计算器
    用函数实现交换的疑问
    结构体变量输入输出的问题
    scanf函数输入float数需要注意的问题
    oracle学习手记(1)
  • 原文地址:https://www.cnblogs.com/qingzhibin/p/upfile.html
Copyright © 2011-2022 走看看