| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY 
共有 635 人关注过本帖
标题:求代码,upload控件上传时改名
收藏  订阅  推荐  打印 
tuablove
Rank: 2
等级:注册会员
帖子:78
积分:880
注册:2005-7-11
求代码,upload控件上传时改名

求代码,upload控件上传时改名求代码,upload控件上传时改名,
以当前时间为新的名称,谢谢
搜索更多相关主题的帖子: 控件  upload  代码  
2007-3-31 12:02
tuablove
Rank: 2
等级:注册会员
帖子:78
积分:880
注册:2005-7-11

大哥大姐,帮帮忙,谢谢

〖石器时代〗来到现代,不好混啊.... @==)(=======> 为生存而拼搏
2007-3-31 12:05
冰镇柠檬汁儿
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:51
帖子:5307
积分:53416
注册:2005-11-7

在saveas的时候写好路径和你想起的文件名就可以了


冰镇柠檬汁儿
2007-3-31 12:10
tuablove
Rank: 2
等级:注册会员
帖子:78
积分:880
注册:2005-7-11

嘿嘿,刚准备给你发短信呢
我这样写的,但是不行
protected void Upload_Click(object sender, EventArgs e)
{
name = FileUpload.FileName;
string type = FileUpload.PostedFile.ContentType;
string size = FileUpload.PostedFile.ContentLength.ToString();
string newname =Convert.ToString( System.DateTime.Now);
string path = Server.MapPath("~/Images/Product/" + newname);

if (type == "image/pjpeg" || type == "image/gif" || type == "image/bmp")
{
FileUpload.SaveAs(path);
Image.ImageUrl = "~/Images/Product/" + newname;
message.Text = "上传名称:" + newname + "大小:" + size + "路径:" + path;
Labelname.Text = newname;
}
else
{
Response.Write("<script>alert('" + "上传类型仅限于gif,jpg,mbp格式图片" + "')</script>");
}

}


哪错了啊?


〖石器时代〗来到现代,不好混啊.... @==)(=======> 为生存而拼搏
2007-3-31 12:16
tuablove
Rank: 2
等级:注册会员
帖子:78
积分:880
注册:2005-7-11

等了好久了,版主帮帮忙

〖石器时代〗来到现代,不好混啊.... @==)(=======> 为生存而拼搏
2007-3-31 13:52
冰镇柠檬汁儿
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:51
帖子:5307
积分:53416
注册:2005-11-7

以下是引用tuablove在2007-3-31 12:16:00的发言:
protected void Upload_Click(object sender, EventArgs e)
{
name = FileUpload.FileName;
string type = FileUpload.PostedFile.ContentType;
string size = FileUpload.PostedFile.ContentLength.ToString();
string newname =Convert.ToString( System.DateTime.Now);
string path = Server.MapPath("../Images/Product/" + newname);

if (type == "image/pjpeg" || type == "image/gif" || type == "image/bmp")
{
FileUpload.SaveAs(path);
Image.ImageUrl = "../Images/Product/" + newname;
message.Text = "上传名称:" + newname + "大小:" + size + "路径:" + path;
Labelname.Text = newname;
}
else
{
Response.Write("<script>alert('" + "上传类型仅限于gif,jpg,mbp格式图片" + "')</script>");
}

}


冰镇柠檬汁儿
2007-4-1 09:40
tuablove
Rank: 2
等级:注册会员
帖子:78
积分:880
注册:2005-7-11

你只引用了但没有回答哦....

〖石器时代〗来到现代,不好混啊.... @==)(=======> 为生存而拼搏
2007-4-1 10:30
HankStar
Rank: 3Rank: 3
等级:中级会员
帖子:230
积分:2500
注册:2006-10-4

Image.ImageUrl = "../Images/Product/" + newname;

2007-4-1 13:20
djx520
Rank: 1
等级:新手上路
威望:1
帖子:38
积分:480
注册:2007-4-2

以前做的,可以用


//Int64 intImageSize;
//string strImageType;
//Stream ImageStream;

//intImageSize = PersonImage.PostedFile.ContentLength;

//strImageType = PersonImage.PostedFile.ContentType;

//ImageStream = PersonImage.PostedFile.InputStream;

//Byte[] ImageContent = new Byte[intImageSize];

//int intStatus = ImageStream.Read(ImageContent, 0, PersonImage.PostedFile.ContentLength);
if (myFile.PostedFile.ContentLength != 0)
{
if (myFile.PostedFile != null)
{
string nam = myFile.PostedFile.FileName;
//Response.Write(nam);
//取得文件名(抱括路径)里最后一个"."的索引

int i = nam.LastIndexOf(".");
//取得文件扩展名

newext = nam.Substring(i);
//这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
DateTime now = DateTime.Now;
newname = now.DayOfYear.ToString() + myFile.PostedFile.ContentLength.ToString();

//保存文件到你所要的目录,这里是IIS根目录下的uploadfiles目录
//注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
myFile.PostedFile.SaveAs(Request.PhysicalApplicationPath + "images\\ftb\\updata\\" + newname + newext);
//myFile.PostedFile.SaveAs("D:\\kmsite\\images\\ftb\\updata\\" + newname + newext);
//myFile.PostedFile.SaveAs(Server.MapPath(".\\UpLoadFiles\\" + newname + newext));
}


DBOp dbop = new DBOp(Request.PhysicalApplicationPath);
int a = dbop.OpenDataBase();
if (a == 1)
{
//int selectr = Convert.ToInt32(this.RadioButtonList1.SelectedValue);
//if (selectr == 1)
//{
OleDbCommand MyCommand = new OleDbCommand("INSERT INTO Main_pricute(Title,laiyuan,author,keyword,content,type,lujing,lanaguagetype) values(@Title,@laiyuan,@author,@keyword,@content,0,'" + "images\\ftb\\updata\\" + newname + newext + "',0)", dbop.DataBaseConn);

MyCommand.Parameters.Add("@Title", txtname.Text.Trim());
MyCommand.Parameters.Add("@laiyuan", TextBox1.Text.Trim());
MyCommand.Parameters.Add("@author", TextBox2.Text.Trim());
MyCommand.Parameters.Add("@keyword", TextBox3.Text.Trim());
MyCommand.Parameters.Add("@content", TextBox4.Text.Trim());
//}
//if (selectr == 2)
//{
// OleDbCommand MyCommand = new OleDbCommand("INSERT INTO Main_pricute(Title,laiyuan,author,keyword,content,type,lujing,lanaguagetype) values(@Title,@laiyuan,@author,@keyword,@content,0,'" + "images\\ftb\\updata\\" + newname + newext + "',0)", dbop.DataBaseConn);

// MyCommand.Parameters.Add("@Title", txtname.Text.Trim());
// MyCommand.Parameters.Add("@laiyuan", TextBox1.Text.Trim());
// MyCommand.Parameters.Add("@author", TextBox2.Text.Trim());
// MyCommand.Parameters.Add("@keyword", TextBox3.Text.Trim());
// MyCommand.Parameters.Add("@content", TextBox4.Text.Trim());
//}
//if (selectr == 3)
//{
// OleDbCommand MyCommand = new OleDbCommand("INSERT INTO Main_pricute(Title,laiyuan,author,keyword,content,type,lujing,lanaguagetype) values(@Title,@laiyuan,@author,@keyword,@content,0,'" + "images\\ftb\\updata\\" + newname + newext + "',0)", dbop.DataBaseConn);

// MyCommand.Parameters.Add("@Title", txtname.Text.Trim());
// MyCommand.Parameters.Add("@laiyuan", TextBox1.Text.Trim());
// MyCommand.Parameters.Add("@author", TextBox2.Text.Trim());
// MyCommand.Parameters.Add("@keyword", TextBox3.Text.Trim());
// MyCommand.Parameters.Add("@content", TextBox4.Text.Trim());
//}
try
{

MyCommand.ExecuteNonQuery();
dbop.DataBaseConn.Close();
//Response.Write("<font color="red">new person successfully added!</font>");
}
catch (OleDbException ex)
{
Response.Write("Insert Failed.Error Details are:" + ex.ToString());
}
}

Response.Write("<script>alert('添加成功!');</script>");
txtname.Text = "";
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
}
else
{
Response.Write("<script>alert('图片不能为空')</script>");
}

2007-4-2 11:14
zhou

等级:禁止发言
帖子:429
积分:4450
注册:2006-6-16
提示: 作者被禁止或删除 内容自动屏蔽
2008-3-31 10:39
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.050593 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved