标题:求代码,upload控件上传时改名
只看楼主
tuablove
Rank: 1
等 级:新手上路
帖 子:78
专家分:0
注 册:2005-7-11
 问题点数:0 回复次数:10 
求代码,upload控件上传时改名
求代码,upload控件上传时改名求代码,upload控件上传时改名,
以当前时间为新的名称,谢谢
搜索更多相关主题的帖子: 控件 upload 代码 
2007-03-31 12:02
tuablove
Rank: 1
等 级:新手上路
帖 子:78
专家分:0
注 册:2005-7-11
得分:0 
大哥大姐,帮帮忙,谢谢

〖石器时代〗来到现代,不好混啊.... @==)(=======> 为生存而拼搏
2007-03-31 12:05
冰镇柠檬汁儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:北京
等 级:版主
威 望:120
帖 子:8074
专家分:6657
注 册:2005-11-7
得分:0 

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


本来无一物,何处惹尘埃
It is empty at all here, Why pm 2.5 is so TMD high!
2007-03-31 12:10
tuablove
Rank: 1
等 级:新手上路
帖 子:78
专家分:0
注 册:2005-7-11
得分:0 

嘿嘿,刚准备给你发短信呢
我这样写的,但是不行
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-03-31 12:16
tuablove
Rank: 1
等 级:新手上路
帖 子:78
专家分:0
注 册:2005-7-11
得分:0 
等了好久了,版主帮帮忙

〖石器时代〗来到现代,不好混啊.... @==)(=======> 为生存而拼搏
2007-03-31 13:52
冰镇柠檬汁儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:北京
等 级:版主
威 望:120
帖 子:8074
专家分:6657
注 册:2005-11-7
得分:0 
以下是引用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>");
}

}


本来无一物,何处惹尘埃
It is empty at all here, Why pm 2.5 is so TMD high!
2007-04-01 09:40
tuablove
Rank: 1
等 级:新手上路
帖 子:78
专家分:0
注 册:2005-7-11
得分:0 
你只引用了但没有回答哦....

〖石器时代〗来到现代,不好混啊.... @==)(=======> 为生存而拼搏
2007-04-01 10:30
HankStar
Rank: 1
等 级:新手上路
帖 子:230
专家分:0
注 册:2006-10-4
得分:0 
Image.ImageUrl = "../Images/Product/" + newname;

2007-04-01 13:20
djx520
Rank: 1
等 级:新手上路
威 望:1
帖 子:38
专家分:0
注 册:2007-4-2
得分:0 

以前做的,可以用


//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-04-02 11:14
zhou
Rank: 1
等 级:禁止发言
帖 子:429
专家分:0
注 册:2006-6-16
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2008-03-31 10:39



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-128308-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.207302 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved