惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
V
V2EX
Cloudbric
Cloudbric
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
量子位
博客园 - 三生石上(FineUI控件)
博客园 - 叶小钗
K
Kaspersky official blog
博客园 - 【当耐特】
T
Tenable Blog
L
Lohrmann on Cybersecurity
The Cloudflare Blog
S
Schneier on Security
A
Arctic Wolf
Latest news
Latest news
C
Cyber Attacks, Cyber Crime and Cyber Security
罗磊的独立博客
T
The Exploit Database - CXSecurity.com
Cisco Talos Blog
Cisco Talos Blog
小众软件
小众软件
P
Privacy & Cybersecurity Law Blog
WordPress大学
WordPress大学
Simon Willison's Weblog
Simon Willison's Weblog
雷峰网
雷峰网
NISL@THU
NISL@THU
人人都是产品经理
人人都是产品经理
月光博客
月光博客
J
Java Code Geeks
V
Visual Studio Blog
S
Security Affairs
博客园 - Franky
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
H
Heimdal Security Blog
有赞技术团队
有赞技术团队
V2EX - 技术
V2EX - 技术
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
T
Troy Hunt's Blog
SecWiki News
SecWiki News
Spread Privacy
Spread Privacy
宝玉的分享
宝玉的分享
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 聂微东

博客园 - 星空竹月

Div中文字竖直居中的问题 ASP入门基础教程-VBScript概述及基本元素 第一次安装sql2000挂起无法安装的问题 asp.net(c#)成条形码[转] 关于文章列表如何直接显示文章类别名的问题 Page_PreInit在网页传值的应用 利用.net的强大功能发送email - 星空竹月 - 博客园 在页面和Datalist控件中判断是否为今天 - 星空竹月 - 博客园 如何在asp.net中关闭B页面时,自动刷新A页面? - 星空竹月 - 博客园 ASP.net(c#)做返回上一页效果(后退)--代码 前进和后退按钮特效代码 - 星空竹月 - 博客园 ASP.NET中文显示乱码之解决方法 限制显示字数 asp.net中,限制显示字数的问题? 将截断字符串或二进制数据。 IE6下透明PNG图片的显示 IE不支持PNG图片透明效果,怎么办 配置AjaxControlToolkit 怎样换网页中的鼠标图案??换成其他图片
文件上传实例(文件上传,自动重命名,自动添加文件夹)
星空竹月 · 2007-09-09 · via 博客园 - 星空竹月

这个问题我以前也已经注意过了,但是都没得到解决,今天在网站找了很多资料,最主要的是得到一个网友的帮助,让我解决了一直困扰我的难题。
前台代码:ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Album_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
            
<asp:FileUpload ID="FileUpload1" runat="server" />
            
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /><br />
            
<asp:Image ID="Image1" runat="server" Height="100px" /><br />
            
<asp:Label ID="Label1" runat="server"></asp:Label></div>
    
</form>
</body>
</html>

后台代码:CS

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Album_Default : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        Image1.Visible 
= false;
    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        
string filename = FileUpload1.FileName;
        
string size = FileUpload1.PostedFile.ContentLength.ToString();
        
string[] myfile = filename.Split('.');
        
string dotname = myfile[myfile.Length - 1].ToString().ToLower();
        
string type = FileUpload1.PostedFile.ContentType;
        
string type2 = filename.Substring(filename.LastIndexOf("."+ 1);
        
string imgpath = Server.MapPath("~/Upimg"+ "\\";
        
string filepath = Server.MapPath("~/Upfile"+ "\\";
        
string folder = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
        

        
if (type2 == "jpg" || type2 == "gif")
        
{
            Image1.Visible 
= true;
            
if (!System.IO.Directory.Exists(imgpath + folder))
            
{//自动生成文件夹
                System.IO.Directory.CreateDirectory(imgpath + folder);
            }


            Random myrdn 
= new Random();//产生随机数
            
//日期,时间,随机数和后缀名
            string newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + myrdn.Next(10000).ToString() + "." + dotname;
            FileUpload1.SaveAs(imgpath 
+ folder + "\\" + newfilename);
            
string wpath = "~\\Upimg\\" + folder + "\\" + newfilename;
            Image1.ImageUrl 
= wpath;
            
//FileUpload1.SaveAs(ipath);
            Label1.Text = "原始文件名" + filename + "<br>存储文件名:" + newfilename + "<br>文件大小" + size + "<br>文件类型" + type2 + "<br>文件后缀" + type + "<br>文件虚拟路径" + wpath;
        }

        
else
        
{
            
if (!System.IO.Directory.Exists(filepath + folder))
            
{//自动生成文件夹
                System.IO.Directory.CreateDirectory(filepath + folder);
            }


            Random myrdn 
= new Random();//产生随机数
            
//日期,时间,随机数和后缀名
            string newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + myrdn.Next(10000).ToString() + "." + dotname;
            
            Image1.Visible 
= false;
            
string wpath = "Upfile\\" + folder + newfilename;
            FileUpload1.SaveAs(filepath 
+ folder + "\\" + newfilename);
            Label1.Text 
= "原始文件名" + filename + "<br>存储文件名:" + newfilename + "<br>文件大小" + size + "<br>文件类型" + type2 + "<br>文件后缀" + type + "<br>文件虚拟路径" + wpath;
        }

    }

}

===========================
再次感谢网名为Clefoo的网友。
总算没白熬,有收获