谁有用fileupload控件上传图片的代码 RT,别太复杂了,只有先判断下文件后缀,如果对的话就用save as 方法保存下图片就可以了,以前我的项目里有,但是找不到了,手写又比较麻烦,请高手共享下代码吧! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string fn = fi.FileName;//取出的fi文件的名称 if (string.IsNullOrEmpty(fn) || fn.Length < 1) { Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('请上传图片!'); </script>"); } else { string pSuffix = fn.Substring(fn.LastIndexOf(".") + 1); if (pSuffix.ToLower() != "jpg" && pSuffix.ToLower() != "bmp" && pSuffix.ToLower() != "png" && pSuffix.ToLower() != "gif") { Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('请上传正确的图片类型!'); </script>"); } else { string pFileName = fn;//设置后的fi文件名 string pPath = Server.MapPath("../images/photo/") + pFileName;//上传到服务器的目录名称 fi.PostedFile.SaveAs(pPath); string pVirtualPath = "images\\photo\\" + pFileName; SqlParameter[] para ={ new SqlParameter("@id", SqlDbType.Int), new SqlParameter("@photoAddress", SqlDbType.NVarChar, 80), new SqlParameter("@photoExplain", SqlDbType.NVarChar, 80) }; para[0].Value = id; para[1].Value = pVirtualPath; para[2].Value = pExplain; SqlHelper.executeNonQuery("update photo set photoAddress=@photoAddress,photoExplain=@photoExplain where id=@id", CommandType.Text, para); Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新成功!'); </script>"); } } 参考http://www.cnblogs.com/weekzero/archive/2006/05/05/392003.html protected void btnGoUp_Click(object sender, EventArgs e) { string str = this.File1.PostedFile.FileName; if (str == "") { Response.Write(bc.MassageBox("请选择上传图片!")); return; } string ss = str.Substring(str.LastIndexOf("\\") + 1); string s = Server.MapPath("~\\photo\\" + ss); path = "~\\photo\\" + ss; if (File.Exists(s)) { Response.Write(bc.MassageBox("该文件已经存在,请重新命名!!!")); return; } this.File1.PostedFile.SaveAs(s); Response.Write(bc.MassageBox("上传成功!")); }这个比较简单 http://download.csdn.net/source/872260 ASP.NET图片的上传 protected void BtnUpload_Click(object sender, EventArgs e) { bool fileOK = false; string path = Server.MapPath("~/Temp/"); if (FileUpload1.HasFile) { String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower(); String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" }; for (int i = 0; i < allowedExtensions.Length; i++) { if (fileExtension == allowedExtensions[i]) { fileOK = true; } } } if (fileOK) { try { FileUpload1.SaveAs(path + FileUpload1.FileName); LabMessage1.Text = "文件上传成功."; LabMessage2.Text = "<b>原文件路径:</b>" + FileUpload1.PostedFile.FileName + "<br />" + "<b>文件大小:</b>" + FileUpload1.PostedFile.ContentLength + "字节<br />" + "<b>文件类型:</b>" + FileUpload1.PostedFile.ContentType + "<br />"; } catch (Exception ex) { LabMessage1.Text = "文件上传不成功."; } } else { LabMessage1.Text = "只能够上传图片文件."; } } 谁能告诉我到底哪儿错了? 两张表联查 发送邮件 求助:求一字符串中引号的个数 关于邮件群发问题 sql语句问题 请问如何把时间2004-8-13 16:36:36 转化为2004-8-13输出 多个站点数据库数据共享的问题! 关于中断asp.net方法问题(是否可以使用线程) 火急:网页所有asp页面不能解析,所有的html页面OK!!可能是什么原因??如何解决??在线! asp.net登出,注销问题? iis发布问题
{
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('请上传图片!'); </script>");
}
else
{
string pSuffix = fn.Substring(fn.LastIndexOf(".") + 1);
if (pSuffix.ToLower() != "jpg" && pSuffix.ToLower() != "bmp" && pSuffix.ToLower() != "png" && pSuffix.ToLower() != "gif")
{
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('请上传正确的图片类型!'); </script>");
}
else
{
string pFileName = fn;//设置后的fi文件名
string pPath = Server.MapPath("../images/photo/") + pFileName;//上传到服务器的目录名称
fi.PostedFile.SaveAs(pPath);
string pVirtualPath = "images\\photo\\" + pFileName; SqlParameter[] para ={ new SqlParameter("@id", SqlDbType.Int), new SqlParameter("@photoAddress", SqlDbType.NVarChar, 80), new SqlParameter("@photoExplain", SqlDbType.NVarChar, 80) };
para[0].Value = id;
para[1].Value = pVirtualPath;
para[2].Value = pExplain;
SqlHelper.executeNonQuery("update photo set photoAddress=@photoAddress,photoExplain=@photoExplain where id=@id", CommandType.Text, para);
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新成功!'); </script>");
}
}
http://www.cnblogs.com/weekzero/archive/2006/05/05/392003.html
protected void btnGoUp_Click(object sender, EventArgs e)
{
string str = this.File1.PostedFile.FileName;
if (str == "")
{
Response.Write(bc.MassageBox("请选择上传图片!"));
return;
} string ss = str.Substring(str.LastIndexOf("\\") + 1);
string s = Server.MapPath("~\\photo\\" + ss);
path = "~\\photo\\" + ss;
if (File.Exists(s))
{
Response.Write(bc.MassageBox("该文件已经存在,请重新命名!!!"));
return;
}
this.File1.PostedFile.SaveAs(s);
Response.Write(bc.MassageBox("上传成功!"));
}这个比较简单
{
bool fileOK = false;
string path = Server.MapPath("~/Temp/");
if (FileUpload1.HasFile)
{
String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}
if (fileOK)
{
try
{
FileUpload1.SaveAs(path + FileUpload1.FileName);
LabMessage1.Text = "文件上传成功.";
LabMessage2.Text = "<b>原文件路径:</b>" + FileUpload1.PostedFile.FileName + "<br />" +
"<b>文件大小:</b>" + FileUpload1.PostedFile.ContentLength + "字节<br />" +
"<b>文件类型:</b>" + FileUpload1.PostedFile.ContentType + "<br />";
}
catch (Exception ex)
{
LabMessage1.Text = "文件上传不成功.";
}
}
else
{
LabMessage1.Text = "只能够上传图片文件.";
}
}