想要一个上传图片的源码 我是刚学这个的,现在在做一个网站要用到这个功能,希望好人能给我一个这样的源码,最好能有注释,这样我就更容易看得懂了,我要的功能很简单:1、可以限制图片的大小。2、能指定上传的格式。3、能实现缩略图。大概就这些,谢谢好人了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string fullname = FileUpload1.FileName.ToString();//直接取得文件名 string url = FileUpload1.PostedFile.FileName.ToString();//取得上传文件路径 string typ = FileUpload1.PostedFile.ContentType.ToString();//获取文件MIME内容类型 string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1);//获取文件名字 . 后面的字符作为文件类型 int size = FileUpload1.PostedFile.ContentLength;//获取文件格式的大小 //下面是保存,判断 if (File.Exists(url)) { Response.Write("<script>alert('文件已存在 !')</script>"); } else { if (typ2 == "gif" || typ2 == "jpg" || typ2 == "bmp" || typ2 == "png" || typ2 == "rar") { if (size <= 4134904) { FileUpload1.SaveAs(Server.MapPath("upload") + "\\" + fullname);//Server.MapPath("uploads\\image") + "\\" + //fullname将文件保存在跟目录的uploads文件夹下 Response.Write("<script>alert('上传成功');</script>"); } else { Response.Write("<script>alert('你的文件超过限制大小!')</script>"); } } else { Response.Write("<script>alert('上传文件格式不正确)</script>"); } } string fileName = this.FileUpload_Import.PostedFile.FileName; string _fileExt = fileName.Substring(fileName.LastIndexOf(".") + 1); if (_fileExt.ToLower() != "txt") { ScriptHelper.AlertAndGoHistory("暂不允许导入除txt文件外的其他格式文件!", -1); Response.End(); } if (this.FileUpload_Import.PostedFile.ContentLength > 2097152) { ScriptHelper.AlertAndGoHistory("导入的文件大小不能超过2MB!", -1); Response.End(); } try { //上传读取导入的txt文件 string filePath = Server.MapPath("/Upload/") + this.FileUpload_Import.FileName; this.FileUpload_Import.PostedFile.SaveAs(filePath); string[] values = File.ReadAllLines(filePath); //删除上传文件 System.IO.FileInfo file = new System.IO.FileInfo(filePath); if (file.Exists) { file.Delete(); } //将数据导入数据库 int success = 0; if (SpreadCustomerInfo.BatchInsertInfo(values, out success)) { ScriptHelper.AlertAndRedirect("导入完成,成功导入" + success.ToString() + "条数据", "/Manager/SMSSpreadUserInfo.aspx"); } else { ScriptHelper.AlertAndRedirect("导入失败!", "/Manager/SMSSpreadUserInfo.aspx"); } } catch (Exception ex) { Logger.Error("导入数据失败:{0}", ex.ToString()); ScriptHelper.AlertAndRedirect("导入数据失败,导入文件错误!", "/Manager/SMSSpreadUserInfo.aspx"); } asp.net上传图片并同时生成缩略图 SQL2005的安装出现问题 索引越界的错误怎么解决? 全角转换为 半角 为什么C#执行会直接跳过一些代码? 请Knight94(愚翁)或hwj_wj_123 (文文)帮下忙? X=X++为什么错? c#.net是否可以调用asp的session 怎么用c#实现windows命令net send啊?大侠帮我啊!! 分不多了,跪求!循环插入记录 我在winform上画出来的带箭头直线为什么是这个样子? 以C#代码为基础,以及数据库和FRID。做一个仓库管理系统的设计 怎么做读取带有特殊字符 XML的功能
string url = FileUpload1.PostedFile.FileName.ToString();//取得上传文件路径
string typ = FileUpload1.PostedFile.ContentType.ToString();//获取文件MIME内容类型
string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1);//获取文件名字 . 后面的字符作为文件类型
int size = FileUpload1.PostedFile.ContentLength;//获取文件格式的大小 //下面是保存,判断
if (File.Exists(url))
{
Response.Write("<script>alert('文件已存在 !')</script>");
}
else
{
if (typ2 == "gif" || typ2 == "jpg" || typ2 == "bmp" || typ2 == "png" || typ2 == "rar")
{
if (size <= 4134904)
{
FileUpload1.SaveAs(Server.MapPath("upload") + "\\" + fullname);//Server.MapPath("uploads\\image") + "\\" + //fullname将文件保存在跟目录的uploads文件夹下
Response.Write("<script>alert('上传成功');</script>");
}
else
{
Response.Write("<script>alert('你的文件超过限制大小!')</script>");
} }
else
{
Response.Write("<script>alert('上传文件格式不正确)</script>"); } }
string _fileExt = fileName.Substring(fileName.LastIndexOf(".") + 1);
if (_fileExt.ToLower() != "txt")
{
ScriptHelper.AlertAndGoHistory("暂不允许导入除txt文件外的其他格式文件!", -1);
Response.End();
}
if (this.FileUpload_Import.PostedFile.ContentLength > 2097152)
{
ScriptHelper.AlertAndGoHistory("导入的文件大小不能超过2MB!", -1);
Response.End();
}
try
{
//上传读取导入的txt文件
string filePath = Server.MapPath("/Upload/") + this.FileUpload_Import.FileName;
this.FileUpload_Import.PostedFile.SaveAs(filePath);
string[] values = File.ReadAllLines(filePath);
//删除上传文件
System.IO.FileInfo file = new System.IO.FileInfo(filePath);
if (file.Exists)
{
file.Delete();
}
//将数据导入数据库
int success = 0;
if (SpreadCustomerInfo.BatchInsertInfo(values, out success))
{
ScriptHelper.AlertAndRedirect("导入完成,成功导入" + success.ToString() + "条数据", "/Manager/SMSSpreadUserInfo.aspx");
}
else
{
ScriptHelper.AlertAndRedirect("导入失败!", "/Manager/SMSSpreadUserInfo.aspx");
}
}
catch (Exception ex)
{
Logger.Error("导入数据失败:{0}", ex.ToString());
ScriptHelper.AlertAndRedirect("导入数据失败,导入文件错误!", "/Manager/SMSSpreadUserInfo.aspx");
}