分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

.NET 图片上传接收类

发布时间:2023-09-06 02:07责任编辑:郭大石关键词:.NET
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.IO;using System.Configuration;using System.Web.Configuration;using System.Collections;using System.Text.RegularExpressions;namespace Visaok.ul.UserCenter.dialogs.attachment{ ???public partial class upfile : System.Web.UI.Page ???{ ???????public string s; ???????private string state = "SUCCESS"; ???????protected void Page_Load(object sender, EventArgs e) ???????{ ???????} ???????protected void Button1_Click(object sender, EventArgs e) ???????{ ???????????string[] filetype = { ".rar", ".doc", ".docx", ".zip", ".pdf", ".txt", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".mov", ".wmv", ".mp4", ".webm",".jpg" }; ???//文件允许格式 ???????????int size = 100; ??//文件大小限制,单位MB,同时在web.config里配置环境默认为100MB ???????????string uploadpath = Request.MapPath("~/"); ???//取得网站根目录路径 ???????????//格式验证 ???????????if (checkType(filetype)) ???????????{ ???????????????//不允许的文件类型 ???????????????state = "\u4e0d\u5141\u8bb8\u7684\u6587\u4ef6\u7c7b\u578b"; ???????????} ???????????//大小验证 ???????????if (checkSize(size)) ???????????{ ???????????????//文件大小超出网站限制 ???????????????state = "\u6587\u4ef6\u5927\u5c0f\u8d85\u51fa\u7f51\u7ad9\u9650\u5236"; ???????????} ???????????//保存图片 ???????????if (state == "SUCCESS") ???????????{ ???????????????//目录创建 ???????????????string Path = DateTime.Now.ToString("yyMMddHHmmss") + (new Random().Next(0, 99).ToString("00")); ???????????????createFolder(uploadpath + "Peerfile\\"+ Path); ???????????????if (string.IsNullOrEmpty(FileUpload1.FileName)) { Response.Write("未选择文件"); return; } ???????????????FileUpload1.SaveAs(uploadpath + "Peerfile\\" + Path+"\\" + FileUpload1.FileName); ?//存储文件到文件夹 ???????????????FileInfo pathinfo = new FileInfo(uploadpath + "Peerfile\\" + Path + "\\" + FileUpload1.FileName.ToString()); ???????????????string pathtwo = WebConfigurationManager.AppSettings["path"]; ???????????????string RootDirectory = WebConfigurationManager.AppSettings["RootDirectory"]; ???????????????s = "&nbsp;&nbsp;&nbsp;&nbsp;<b><a href=\"" + pathtwo + Path + "/" + pathinfo.Name + "\"><img style=\"display:inline-block;\" src=\"" + RootDirectory + "Peer/PeerCenter/dialogs/attachment/images/icon_rar.gif\">" + pathinfo.Name + "</a></b>&nbsp;&nbsp;&nbsp;&nbsp;"; ???????????????div1.InnerHtml = pathinfo.Name + "上传成功"; ???????????} ???????????else ???????????{ ???????????????div1.InnerHtml = "上传的格式不允许"; ???????????} ???????} ???/** ???* 获取文件信息 ???* @param context ???* @param string ???* @return string ???*/ ???????public string getOtherInfo(HttpContext cxt, string field) ???????{ ???????????string info = null; ???????????if (cxt.Request.Form[field] != null && !String.IsNullOrEmpty(cxt.Request.Form[field])) ???????????{ ???????????????info = field == "fileName" ? cxt.Request.Form[field].Split(‘,‘)[1] : cxt.Request.Form[field]; ???????????} ???????????return info; ???????} ????????/** ????????* 重命名文件 ????????* @return string ????????*/ ???????private string reName() ???????{ ???????????return System.Guid.NewGuid() + getFileExt(); ???????} ???????/** ????????* 文件类型检测 ????????* @return bool ????????*/ ???????private bool checkType(string[] filetype) ???????{ ??????????string currentType = getFileExt(); ???????????return Array.IndexOf(filetype, currentType) == -1; ???????} ???????/** ????????* 文件大小检测 ????????* @param int ????????* @return bool ????????*/ ???????private bool checkSize(int size) ???????{ ???????????//ContentLength ???????????return FileUpload1.PostedFile.ContentLength>= (size * 1024 * 1024); ???????} ???????/** ????????* 获取文件扩展名 ????????* @return string ????????*/ ???????private string getFileExt() ???????{ ???????????string[] temp = FileUpload1.FileName.Split(‘.‘); ???????????return "." + temp[temp.Length - 1].ToLower(); ???????} ???????/** ????????* 按照日期自动创建存储文件夹 ????????*/ ???????private void createFolder(string fintPath) ???????{ ???????????if (!Directory.Exists(fintPath)) ???????????{ ???????????????Directory.CreateDirectory(fintPath); ???????????} ???????} ???????/** ????????* 删除存储文件夹 ????????* @param string ????????*/ ???????public void deleteFolder(string path) ???????{ ???????????//if (Directory.Exists(path)) ???????????//{ ???????????// ???Directory.Delete(path, true); ???????????//} ???????} ???????public static string Format(string format, string filename) ???????{ ???????????if (String.IsNullOrWhiteSpace(format)) ???????????{ ???????????????format = "{filename}{rand:6}"; ???????????} ???????????string ext = Path.GetExtension(filename); ???????????filename = Path.GetFileNameWithoutExtension(filename); ???????????format = format.Replace("{filename}", filename); ???????????format = new Regex(@"\{rand(\:?)(\d+)\}", RegexOptions.Compiled).Replace(format, ???????????new MatchEvaluator(delegate(Match match) ???????????{ ???????????????var digit = 6; ???????????????if (match.Groups.Count >2) ???????????????{ ???????????????????digit =Convert.ToInt32(match.Groups[2].Value); ???????????????} ???????????????var rand = new Random(); ???????????????return rand.Next((int)Math.Pow(10,digit),(int)Math.Pow(10,digit +1)).ToString(); ???????????})); ???????????format = format.Replace("{time}", DateTime.Now.Ticks.ToString()); ???????????format = format.Replace("{yyyy}", DateTime.Now.Year.ToString()); ???????????format = format.Replace("{yy}", (DateTime.Now.Year % 100).ToString("D2")); ???????????format = format.Replace("{mm}", DateTime.Now.Month.ToString("D2")); ???????????format = format.Replace("{dd}", DateTime.Now.Day.ToString("D2")); ???????????format = format.Replace("{hh}", DateTime.Now.Hour.ToString("D2")); ???????????format = format.Replace("{ii}", DateTime.Now.Minute.ToString("D2")); ???????????format = format.Replace("{ss}", DateTime.Now.Second.ToString("D2")); ???????????var invalidPattern = new Regex(@"[\\\/\:\*\?\042\<\>\|]"); ???????????format = invalidPattern.Replace(format, ""); ???????????return format + ext; ???????} ???}}

.NET 图片上传接收类

原文地址:https://www.cnblogs.com/Xanthus/p/9595614.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved