您的位置:首页 > 编程语言

一个文件上传的示例代码

2009-06-22 11:49 489 查看
string savePath = "~/images/upload/";
//开始上传
if (this.FileUpload1.HasFile)
{
//rd.Next();
string fileName = this.FileUpload1.FileName;
//更改文件名称
int index = fileName.LastIndexOf('.') + 1;
//文件的后缀名
string endName = fileName.Substring(index, fileName.Length - index);
//文件前缀名
string startName = DateTime.Now.ToFileTimeUtc().ToString();
StringBuilder sb = new StringBuilder(startName);
//给前缀名插入随即字符
Random rd = new Random();
for (int i = 0; i < 10; i++)
{
int strIndex = rd.Next(0, startName.Length - 1);
sb.Insert(strIndex, Convert.ToChar(rd.Next(97, 122)).ToString());
}
//获取当前系统的毫秒数时间,并且创建一个新名称
string newName = sb.ToString() + "." + endName;
//保存文件路径
savePath += newName;
this.FileUpload1.SaveAs(MapPath(savePath));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: