以当前时间为新的名称,谢谢
嘿嘿,刚准备给你发短信呢
我这样写的,但是不行
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>");
}
}
哪错了啊?
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>");
}
}
以前做的,可以用
//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>");
}