Asp.net最基本的文件上传功能代码

aspx前台页面代码

    <form id="form1" action="File.aspx" runat="server" enctype="multipart/form-data">
    <div>
    <input type="file" id="filename" name="filename" value="" /><input type="submit" value="上传" />
    </div>
    </form

后台页面代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 namespace FileUpload
 9 {
10     public partial class File : System.Web.UI.Page
11     {
12         protected void Page_Load(object sender, EventArgs e)
13         {
14             if (IsPostBack)
15             {
16                 HttpPostedFile hp = Request.Files["filename"];
17                 string filename = hp.FileName;
18                 string[] filenum = filename.Split('.');
19                 string[] newnum = filenum[0].Split(':');
20                 string[] newnums = newnum[newnum.Length - 1].Split('\\');
21                 string filextention = filenum[filenum.Length - 1];
22 
23                 string newFilename = newnums[newnums.Length - 1] + DateTime.Now.Year + DateTime.Now.Month.ToString() + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + "." + filextention;
24                 string path =Request.MapPath("/Files/") + newFilename;
25                 hp.SaveAs(path);
26                 
27             }
28         }
29     }
30 }

 

转载于:https://www.cnblogs.com/alphafly/p/3676732.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值