博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图片上传代码
阅读量:7005 次
发布时间:2019-06-28

本文共 3721 字,大约阅读时间需要 12 分钟。

public class UpLoadFile:System.Web.UI.Page    {            public UpLoadFile()            {                //        // TODO: 在此处添加构造函数逻辑        //            }          ///             /// 文件上传            ///             /// 上传控件            /// 源图片名子            /// 那个页面的操作            /// 
返回上传的文件名,可以为空
public static string UpLoadFileImg(HttpPostedFile UpFile, string SourceImg, System.Web.UI.Page SourcePage) { //锁定页面 SourcePage.Application.Lock(); string Img = UpFile.FileName.Trim();//获取文件名 string WebPath = SourcePage.Server.MapPath("UpLoadFile/UsersPhoto/");//上传到指定路径 string Exten = Path.GetExtension(UpFile.FileName).ToUpper();//获取文件的扩展名 int FileLength = UpFile.ContentLength;//文件大小 string FileType = UpFile.ContentType.ToUpper();//获取文件的类型 if (Img != "") { //上传文件第一级文件扩展名和类型验证 if (Exten != ".GIF" && Exten != ".JPG" && FileType != "" && FileType != "") { Img = "No"; WebScript.JavaScript.OnlyAlertMsg(SourcePage, "上传文件格式只能是(.jpg||.gif)格式!"); } else if (FileLength / 1024 / 1024 > 1) //上传文件不能大于1M { Img = "No"; WebScript.JavaScript.OnlyAlertMsg(SourcePage, "上传图片不能超过1M"); } else { Img = DateTime.Now.ToString().Replace(" ", "").Replace(":", "").Replace("-", "") + Exten; //以当前时间来命名 //上传文件 UpFile.SaveAs(WebPath + Img); //最后一部高级验证,图片上传后的操作,判断是否真的是图片 StreamReader sr = new StreamReader(WebPath + Img, Encoding.Default); string strContent = sr.ReadToEnd(); sr.Close(); string str = "request|script|.getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas|wscript.shell|script.encode|server.|.createobject|execute|activexobject|language="; foreach (string s in str.Split('|')) if (strContent.IndexOf(s) != -1) { File.Delete(WebPath + Img); Img = "No"; WebScript.JavaScript.OnlyAlertMsg(SourcePage, "这张图片格式非法,请换一张,谢谢!"); break; } //删除源文件 if (Img != "No" && File.Exists(WebPath + SourceImg)) File.Delete(WebPath + SourceImg);//如果文件已经存在就删除 } } else Img = SourceImg; //取消锁定页面 SourcePage.Application.UnLock(); return Img; } }

 

 
//锁定读取  using System.IO;
using System.Text;public string fileoper(string filepath){  using(streamreader sr=File.OpenText(filepath))  {     string s="";     while((s=sr.readline())!=null)    {       Response.Write(s);    }  }}

转载于:https://www.cnblogs.com/Rampage/archive/2012/10/11/2719951.html

你可能感兴趣的文章
一切都在哲学中
查看>>
我的友情链接
查看>>
PDF Creator v5.0 支持手势和触屏
查看>>
make gridView's cell as square
查看>>
js返回相对时间
查看>>
Spring中 @Autowired注解与@Resource注解的区别
查看>>
你必须知道的.NET:内存分配
查看>>
struct.error: 'h' format requires -32768 number 32767
查看>>
nginx报Could not build the server_names_hash,server_names_hash_bucket_size:32错
查看>>
linux中的计划任务
查看>>
码云全面改版:新界面新态度,匠心凝聚!
查看>>
【码云周刊第 28 期】计算机视觉时代的识图技术
查看>>
如何在IIS7或IIS7.5中导入导出站点及应用程序池.
查看>>
http的缓存机制
查看>>
linux安装mysql二进制包( 完整流程 )
查看>>
百度富文本编辑器插入html代码
查看>>
Jquery文本框变色
查看>>
再学 GDI+[26]: TGPPen - 画笔对齐 - SetAlignment
查看>>
10.位图索引
查看>>
图解 CSS (4): background - 背景
查看>>