我想知道,在asp.net中如何实现点击一个按钮,获取图片路径,然后在网页上动态添加img控件,并将对应路径
展开全部
protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "up")
{
FileUpload fup = DetailsView1.FindControl("FileUpload1") as FileUpload;
string dirPath = "admin/images/UpFile/";
string dirPath2 = "images/UpFile/";
string strRs = Common.UpFile("~/" + dirPath, fup, Label2);
if (strRs != null)
{
((HiddenField)DetailsView1.FindControl("hidImage")).Value = dirPath2 + strRs;
((Image)DetailsView1.FindControl("Image1")).ImageUrl = "~/" + dirPath + strRs;
}
}
}
这个是赋值的
-----------------------
这个是图片上传
public static string UpFile(string dirPath, FileUpload FileUpload1, Label Label1)
{
if (!Directory.Exists(HttpContext.Current.Server.MapPath(dirPath)))
{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(dirPath));
}
if (FileUpload1.PostedFile.ContentType == "image/jpg" || FileUpload1.PostedFile.ContentType == "image/gif" || FileUpload1.PostedFile.ContentType == "image/jpeg" || FileUpload1.PostedFile.ContentType == "image/pjpeg")
{
if (FileUpload1.PostedFile.ContentLength > 500 * 1024)
{
Label1.Text = Common.Alert("图片大小不正确!");
return null;
}
else
{
string pic = Guid.NewGuid().ToString();
string pictype = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf("."));
string picname = pic + pictype;
string desc = HttpContext.Current.Server.MapPath(dirPath);
FileUpload1.SaveAs(desc + picname);
return picname;
}
}
else
{
Label1.Text = Common.Alert("图片类型不正确!");
return null;
}
}
具体路径什么的自己修改
{
if (e.CommandName == "up")
{
FileUpload fup = DetailsView1.FindControl("FileUpload1") as FileUpload;
string dirPath = "admin/images/UpFile/";
string dirPath2 = "images/UpFile/";
string strRs = Common.UpFile("~/" + dirPath, fup, Label2);
if (strRs != null)
{
((HiddenField)DetailsView1.FindControl("hidImage")).Value = dirPath2 + strRs;
((Image)DetailsView1.FindControl("Image1")).ImageUrl = "~/" + dirPath + strRs;
}
}
}
这个是赋值的
-----------------------
这个是图片上传
public static string UpFile(string dirPath, FileUpload FileUpload1, Label Label1)
{
if (!Directory.Exists(HttpContext.Current.Server.MapPath(dirPath)))
{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(dirPath));
}
if (FileUpload1.PostedFile.ContentType == "image/jpg" || FileUpload1.PostedFile.ContentType == "image/gif" || FileUpload1.PostedFile.ContentType == "image/jpeg" || FileUpload1.PostedFile.ContentType == "image/pjpeg")
{
if (FileUpload1.PostedFile.ContentLength > 500 * 1024)
{
Label1.Text = Common.Alert("图片大小不正确!");
return null;
}
else
{
string pic = Guid.NewGuid().ToString();
string pictype = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf("."));
string picname = pic + pictype;
string desc = HttpContext.Current.Server.MapPath(dirPath);
FileUpload1.SaveAs(desc + picname);
return picname;
}
}
else
{
Label1.Text = Common.Alert("图片类型不正确!");
return null;
}
}
具体路径什么的自己修改
追问
我想知道这个里面出现的Common.Alert是什么?需要引入什么js文件么? 我这边报错说common中不存在Alert方法
追答
public static string Alert(string msg)
{
//return ""+msg+"";
return string.Format("alert('{0}');", msg);
}
这个是弹窗的..光想着上传了...
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询