注册 登录
编程论坛 ASP.NET技术论坛

求代码,upload控件上传时改名

tuablove 发布于 2007-03-31 12:02, 1857 次点击
求代码,upload控件上传时改名求代码,upload控件上传时改名,
以当前时间为新的名称,谢谢
10 回复
#2
tuablove2007-03-31 12:05
大哥大姐,帮帮忙,谢谢
#3
冰镇柠檬汁儿2007-03-31 12:10

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

#4
tuablove2007-03-31 12:16

嘿嘿,刚准备给你发短信呢
我这样写的,但是不行
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>");
}

}


哪错了啊?

#5
tuablove2007-03-31 13:52
等了好久了,版主帮帮忙
#6
冰镇柠檬汁儿2007-04-01 09:40
以下是引用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>");
}

}

#7
tuablove2007-04-01 10:30
你只引用了但没有回答哦....
#8
HankStar2007-04-01 13:20
Image.ImageUrl = "../Images/Product/" + newname;
#9
djx5202007-04-02 11:14

以前做的,可以用


//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>");
}

#10
zhou2008-03-31 10:39
提示: 作者被禁止或删除 内容自动屏蔽,只有管理员可见
#11
残影留香2008-03-31 10:46
应该是这样!
string fullname = this.File1.PostedFile.FileName;//取出上传文件的全部路径
        string filename = fullname.Substring(fullname.LastIndexOf("\\")+1);//截取最后一层目录下的文件名
        filename = System.DateTime.Now.ToString().Replace("-", "").Replace(" ", "").Replace(":", "");//给文件以当前日期时间重命名文件
        string type = fullname.Substring(fullname.LastIndexOf(".") + 1);//截取文件的后缀名
        filename = System.DateTime.Now.ToString().Replace("-", "").Replace(" ", "").Replace(":", "") +"."+ type;
        if (type == "jpg" || type == "gif" || type == "bmp")//判断上传文件的格式
        {
            this.File1.PostedFile.SaveAs(Server.MapPath("uploads") + "\\" + filename);
            this.Image1.ImageUrl = "uploads/" + filename;
            this.TextBox1.Text = "uploads/"+filename;
        }
        else
        {
            Response.Write("<script language=javascript>alert('你选择的图片格式不正确!');</script>");
        }
1