zoukankan
html css js c++ java
C#实现按日期命名上传文件代码
c#实现按日期命名上传文件代码,做开发的应该能够用得了吧!
“附件说明”边的TextBox:Id=TextBox1
“浏览”:Id=UpLoadFile
“上传”:Id=AddFile
文件目录:wwwroot/myWeb/userfiles/upload
private void AddFile_Click(object sender, System.EventArgs e)
{
if(UpLoadFile.PostedFile.FileName.Trim()!="")
{
String fileName =UpLoadFile.PostedFile.FileName.Substring (UpLoadFile.PostedFile.FileName.LastIndexOf("\\")+1, UpLoadFile.PostedFile.FileName.Length-1 - UpLoadFile.PostedFile.FileName.LastIndexOf("\\"));
///取到当前时间的年、月、日、分、秒和毫秒的值,并使用字符串格式把它们组合成一个字符串
String fileTime = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString()
+ DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString()
+ DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString()
+ DateTime.Now.Millisecond.ToString();
///在时间字符串后面添加一个随机数和文件的后缀名
String src=fileName.Substring(fileName.LastIndexOf(".")+1,fileName.Length-fileName.LastIndexOf(".")-1)。ToLower();
if(src.ToLower()=="rar" || src.ToLower()=="doc" || src.ToLower()=="xls")
{
fileName =fileTime + GetRandomint() + "." + src;
///上载文件到服务器硬盘
UpLoadFile.PostedFile.SaveAs(Server.MapPath(Request.ApplicationPath) + "\\userfiles\\upload\\" + fileName);
if(TextBox1.Text.Trim()=="")
{
FCKeditor1.Value=FCKeditor1.Value+"<a href=userfiles/upload/"+fileName+">下载附件</a><br>";
}
else
{
FCKeditor1.Value=FCKeditor1.Value+"<a href=userfiles/upload/"+fileName+">"+TextBox1.Text.ToString()+"< /a><br>";
}
}
else
{
Response.Write("<script>alert(\"文件格式不正确,请上传格式为RAR的文件!\")</script>");
}
}
}
private String GetRandomint()
{
Random random = new Random();
return(random.Next(10000)。ToString()); //产生一个小于10000的随机正整数
}
查看全文
相关阅读:
二次开发注意
LAMP集群项目五 nfs分发文件到服务器
LAMP集群项目五 nfs存储的数据实时同步到backupserver
LAMP集群项目五 项目备份
LAMP集群项目五 部署NFS存储服务并设置WEB服务挂载
LAMP集群项目四 安装apache、php及其插件
iOS-单选cell的实现
iOS-省市区选择的实现
随机颜色的产生
刷新轮的使用
原文地址:https://www.cnblogs.com/yzenet/p/2499368.html
最新文章
2018年5月5日论文阅读
C++ code:for loop designs
C++ code:prime decision
C++ one more time
视觉显著性检测(Visual saliency detection)相关概念
《MATLAB Deep Learning:With Machine Learning,Neural Networks and Artificial Intelligence》选记
shell脚本学习总结11--脚本调试
Linux命令之乐--md5sum
shell脚本学习总结10--系统函数调用
使用openstack部署云计算服务环境
热门文章
Linux产生随机数的几种常见方法
ssh通过密钥进行连接
LVM磁盘管理
service xxx does not support chkconfig
Linux命令之乐--telnet
Linux命令之乐--curl
MySQL数据库的基本操作
MySQL自定义函数
Eclipse启动Server报错:Could not publish to the server. java.lang.NullPointerException
eclipse中的SVN文件还原到历史版本
Copyright © 2011-2022 走看看