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

推荐订阅源

D
Docker
Microsoft Azure Blog
Microsoft Azure Blog
云风的 BLOG
云风的 BLOG
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LangChain Blog
P
Privacy & Cybersecurity Law Blog
Hugging Face - Blog
Hugging Face - Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
A
Arctic Wolf
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
The GitHub Blog
The GitHub Blog
P
Privacy International News Feed
WordPress大学
WordPress大学
U
Unit 42
S
Securelist
T
The Exploit Database - CXSecurity.com
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
Latest news
Latest news
Hacker News: Ask HN
Hacker News: Ask HN
小众软件
小众软件
Know Your Adversary
Know Your Adversary
The Cloudflare Blog
V
Vulnerabilities – Threatpost
The Hacker News
The Hacker News
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
Security Latest
Security Latest
Google DeepMind News
Google DeepMind News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Simon Willison's Weblog
Simon Willison's Weblog
博客园 - Franky
Y
Y Combinator Blog
博客园 - 叶小钗
Security Archives - TechRepublic
Security Archives - TechRepublic
Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
S
Secure Thoughts
T
Threat Research - Cisco Blogs
aimingoo的专栏
aimingoo的专栏
S
SegmentFault 最新的问题
Microsoft Security Blog
Microsoft Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 司徒正美
M
MIT News - Artificial intelligence

博客园 - 星空竹月

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的网友。
总算没白熬,有收获