还是没有找到答案(C#)

来源:百度知道 编辑:UC知道 时间:2024/06/23 08:59:26
代码如下:
public partial class 保存控件的状态 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
if (this.file.PostedFile.FileName.Trim() != "")
{
if (this.file.PostedFile.ContentLength / 2048 > 2048)
{

Page.RegisterStartupScript("", "<script language=javascript>alert('图片不能大于2M!');history.back(-1);</script>");

}
else
{
string extension = Path.GetExtension(file.PostedFile.FileName).ToUpper();
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
string path = Server.MapPath("") + "/upfile/" + fileName + extension;
file.PostedFile.SaveAs(path);
Response.Write("上传成功!");
}

}
else
{
Response.Write("<script language=javascript>alert('

不知道你的图片预览是怎么做.

我给你个思路.很简单的.

<input type=file id="myfile" onpropertychange="test(this.value)"><br>
<img id="myimg" src="">
<script>
function test(val){
document.getElementById("myimg").src=val;
}
</script>

上面的代码是写在前台.当用户浏览图片的时候产生的预览.

而你在后台

Response.Write("<script language=javascript>alert('图片不能为空!')");

这里只需要再调用一次这个js就OK了.如:

Response.Write("<script language=javascript>alert('图片不能为空!');test("+这里是你想要再显示的图片路径变量+")");

不知道你明白了么.

顺便说一下.你怎么没有判断上传文件的格式?

这么判断:

if(file.PostedFile.ContentType.ToLower().IndexOf("image")<0){
Response.Write("<script language=javascript>alert('文件格式不正确')");
}