求一个asp.net上传图片的例子? 上传图片后 显示上传成功后 可以加入数据库(sqlserver) 并且可以在img控件上显示上传的图片(显示控件和上传控件 在一个页面上) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string imagePath = ""; if (FileUploadImage.HasFile) { string FileAndExtName = FileUploadImage.FileName; //获取文件名 string ExtName = " "; string FileName = " "; string ContentType = FileUploadImage.PostedFile.ContentType; //获取文件类型 string filepath = Server.MapPath("~/Upfile") + "\\"; int i = FileAndExtName.LastIndexOf("."); //取得文件扩展名 string newext = FileAndExtName.Substring(i); //上传图片 DateTime now = DateTime.Now; string newname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString(); if (CheckFileType(newext)) { if (!Directory.Exists(filepath)) { Directory.CreateDirectory(filepath); if (Path.HasExtension(FileAndExtName)) { ExtName = Path.GetExtension(FileAndExtName); FileName = FileAndExtName.Replace(ExtName, " "); } else { FileName = FileAndExtName; } FileUploadImage.SaveAs(filepath + newname + newext); } else { if (Path.HasExtension(FileAndExtName)) { ExtName = Path.GetExtension(FileAndExtName); FileName = FileAndExtName.Replace(ExtName, " "); } else { FileName = FileAndExtName; } FileUploadImage.SaveAs(filepath + newname + newext); } } else { Response.Write("<script>alert('图片格式不正确!')</script>"); return; } imagePath = "~/Upfile/" + newname + newext; } else { imagePath = "无"; } bool CheckFileType(string ext)//判断上传图片扩展名 { switch (ext.ToLower()) { case ".gif": case ".png": case ".jpg": case ".jpeg": case ".bmp": return true; default: return false; } }这是显示图片的代码: function GetImagePath() { var upload = document.getElementById("ctl00_ContentPlaceHolder1_FileUploadImage"); var image = document.getElementById("ctl00_ContentPlaceHolder1_Image1"); var uploadValue = upload.value.toString();//debugger var i =uploadValue.lastIndexOf('.'); var houZui = uploadValue.substring(i); if((houZui.toLowerCase()!=".jpg")&&(houZui.toLowerCase()!=".bmp")) { alert("您输入的图片格式不正确");return false; } image.src=upload.value; } if (CheckFileType(newext)) { if (!Directory.Exists(filepath)) { Directory.CreateDirectory(filepath); if (Path.HasExtension(FileAndExtName)) { ExtName = Path.GetExtension(FileAndExtName); FileName = FileAndExtName.Replace(ExtName, " "); } else { FileName = FileAndExtName; } FileUploadImage.SaveAs(filepath + newname + newext); } else { if (Path.HasExtension(FileAndExtName)) { ExtName = Path.GetExtension(FileAndExtName); FileName = FileAndExtName.Replace(ExtName, " "); } else { FileName = FileAndExtName; } FileUploadImage.SaveAs(filepath + newname + newext); } } 哦,你是不是不知道哪个是上传控件?FileUpLoad treeview 问题 <span>标签中的id属性有什么用 asp.net读注册表问题,高手过来看一下 如何取得出生年月的日期值? Asp.net连接Access数据库的问题??? 一个轮班的存储过程 各位高手请给我说说整个ASP。NET的运作流程 web.configue的问题 思归进来啊!活动目录的问题。其他人能解决的也进来阿!!! Oxygen Sms ActiveX Control V2.3 ?? asp.net下生成的安装包安装时出现以下问题,希望高手帮忙看下!! 关于计算点击次数的问题(有谁还有更好的方法)
if (FileUploadImage.HasFile)
{
string FileAndExtName = FileUploadImage.FileName; //获取文件名
string ExtName = " ";
string FileName = " ";
string ContentType = FileUploadImage.PostedFile.ContentType; //获取文件类型
string filepath = Server.MapPath("~/Upfile") + "\\";
int i = FileAndExtName.LastIndexOf("."); //取得文件扩展名
string newext = FileAndExtName.Substring(i);
//上传图片
DateTime now = DateTime.Now;
string newname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
if (CheckFileType(newext))
{
if (!Directory.Exists(filepath))
{
Directory.CreateDirectory(filepath);
if (Path.HasExtension(FileAndExtName))
{
ExtName = Path.GetExtension(FileAndExtName);
FileName = FileAndExtName.Replace(ExtName, " ");
}
else
{
FileName = FileAndExtName;
}
FileUploadImage.SaveAs(filepath + newname + newext);
}
else
{
if (Path.HasExtension(FileAndExtName))
{
ExtName = Path.GetExtension(FileAndExtName);
FileName = FileAndExtName.Replace(ExtName, " ");
}
else
{
FileName = FileAndExtName;
}
FileUploadImage.SaveAs(filepath + newname + newext);
}
}
else
{
Response.Write("<script>alert('图片格式不正确!')</script>");
return;
}
imagePath = "~/Upfile/" + newname + newext;
}
else
{
imagePath = "无";
}
bool CheckFileType(string ext)//判断上传图片扩展名
{
switch (ext.ToLower())
{
case ".gif":
case ".png":
case ".jpg":
case ".jpeg":
case ".bmp":
return true;
default:
return false;
}
}
这是显示图片的代码:
function GetImagePath()
{
var upload = document.getElementById("ctl00_ContentPlaceHolder1_FileUploadImage");
var image = document.getElementById("ctl00_ContentPlaceHolder1_Image1");
var uploadValue = upload.value.toString();
//debugger
var i =uploadValue.lastIndexOf('.');
var houZui = uploadValue.substring(i);
if((houZui.toLowerCase()!=".jpg")&&(houZui.toLowerCase()!=".bmp"))
{
alert("您输入的图片格式不正确");return false;
}
image.src=upload.value;
}
{
if (!Directory.Exists(filepath))
{
Directory.CreateDirectory(filepath);
if (Path.HasExtension(FileAndExtName))
{
ExtName = Path.GetExtension(FileAndExtName);
FileName = FileAndExtName.Replace(ExtName, " ");
}
else
{
FileName = FileAndExtName;
}
FileUploadImage.SaveAs(filepath + newname + newext);
}
else
{
if (Path.HasExtension(FileAndExtName))
{
ExtName = Path.GetExtension(FileAndExtName);
FileName = FileAndExtName.Replace(ExtName, " ");
}
else
{
FileName = FileAndExtName;
}
FileUploadImage.SaveAs(filepath + newname + newext);
}
}
FileUpLoad