|
String filename = Path.GetFileName(FileUpload1.PostedFile.FileName).ToString();
//制定根路径
string path = Server.MapPath("/NewFolder1/");
TextBox1.Text = path.ToString() + filename.ToString();
this.Image1.ImageUrl = path.ToString() + filename.ToString();
//利用时间的无重复性对图片命名
string rename = System.DateTime.Now.ToString("yyyy-MM-dd");
TextBox2.Text = rename;
//判断是否存在此文件
if (FileUpload1.HasFile)
{
//获取扩展名
string str = Path.GetExtension(FileUpload1.PostedFile.FileName.ToLowerInvariant());
if (str == ".bmp" || str == ".png" || str == ".jpeg" || str == ".gif" || str == ".jpg")
{
if (FileUpload1.PostedFile.ContentLength >= 1200000)
{
Response.Write("<script>alert('上传文件不能大于1200kb');history.go(-1)</script>");
}
else
{
//判断是否存在此路径
if (Directory.Exists(path))
{
FileUpload1.PostedFile.SaveAs(path + rename + filename);
string strsql = "Data Source=(local) ;Initial Catalog=hh;Integrated Security=True";
SqlConnection conn = new SqlConnection(strsql);
conn.Open();
int FileLen = FileUpload1.PostedFile.ContentLength;
Byte[] photo1 = new Byte[FileLen];
HttpPostedFile hp = FileUpload1.PostedFile;
Stream sr = hp.InputStream;
int n = sr.Read(photo1, 0, FileLen);
SqlCommand cmd = new SqlCommand("Insert into zonglei(简介,photo,日期) values('" + TextBox1.Text + "', @photo ," + rename + " )", conn);
// cmd.Parameters.Add("@简介", SqlDbType.Text, 16).Value = TextBox1.Text;
// cmd.Parameters.Add("@日期", SqlDbType.DateTime).Value = TextBox2.Text;
SqlParameter para = new SqlParameter("@photo", SqlDbType.Image);
cmd.Parameters.Add(para).Value = photo1;
cmd.ExecuteNonQuery();
Response.Write("<script language=javascript>alert('上传成功!')</script>");
conn.Close();
}
else
{
Directory.CreateDirectory(path);
FileUpload1.PostedFile.SaveAs(path + rename + filename);
}
}
}
else
{
Response.Write("<script>alert('文件上传格式不正确');history.go(-1)</script>");
}
}
else
{
Response.Write("<script>alert('请选择上传文件');history.go(-1)</script>");
}
}
catch
{
Response.Write("<script>alert('操作有错误,请重新选择!');history.go(-1)</script>");
return;
}
哪位大神,帮忙解决一下,图像控件Image ,怎么显示不了图像呢? |
|