.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 = ; //文件大小限制,单位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(, ).ToString(""));
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 = " <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> ";
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(',')[] : 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) == -;
} /**
* 文件大小检测
* @param int
* @return bool
*/ private bool checkSize(int size)
{
//ContentLength
return FileUpload1.PostedFile.ContentLength>= (size * * );
} /**
* 获取文件扩展名
* @return string
*/ private string getFileExt()
{
string[] temp = FileUpload1.FileName.Split('.');
return "." + temp[temp.Length - ].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 = ;
if (match.Groups.Count >)
{
digit =Convert.ToInt32(match.Groups[].Value);
}
var rand = new Random();
return rand.Next((int)Math.Pow(,digit),(int)Math.Pow(,digit +)).ToString();
}));
format = format.Replace("{time}", DateTime.Now.Ticks.ToString());
format = format.Replace("{yyyy}", DateTime.Now.Year.ToString());
format = format.Replace("{yy}", (DateTime.Now.Year % ).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 图片上传接收类的更多相关文章
- ASP.NET 图片上传工具类 upload image简单好用功能齐全
使用方法: UploadImage ui = new UploadImage(); /***可选参数***/ ui.SetWordWater = "哈哈";//文字水印 // ui ...
- PHP 图片上传工具类(支持多文件上传)
====================ImageUploadTool======================== <?php class ImageUploadTool { private ...
- 基于WebImage的图片上传工具类
支持缩略图和水印. using System; using System.IO; using System.Linq; using System.Web; using System.Web.Helpe ...
- ashx图片上传接收
发送数据流方法 /// <summary> /// PostBinaryData /// </summary> /// <param name="url&quo ...
- JSP图片上传 公共工具类
需要jsmartcom_zh_CN.jar支持. 下载地址: http://files.cnblogs.com/simpledev/jsmartcom_zh_CN.rar <%@page imp ...
- PHP图片上传类
前言 在php开发中,必不可少要用到文件上传,整理封装了一个图片上传的类也很有必要. 图片上传的流程图 一.控制器调用 public function upload_file() { if (IS_P ...
- android批量文件上传(android批量图片上传)
项目中多处用到文件批量上传功能,今天正好解决了此问题,在此写出来,以便日后借鉴. 首先,以下架构下的批量文件上传可能会失败或者不会成功: 1.android客户端+springMVC服务端:服务端 ...
- thinkjs升级到3.0后的图片上传
似乎当thinkjs升级到3.0后,才接手了一个项目.只是在实际运用过程中,还是发现了与2.2的些许差别——今天先分享关于图片上传的一些问题. 1.上传文件,我们选择了jQuery的插件:http:/ ...
- spring boot图片上传至远程服务器并返回新的图片路径
界面上传图片时考虑到可能会有用户的图片名称一致,使用UUID来对图片名称进行重新生成. //UUIDUtils public class UUIDUtils { public static Strin ...
随机推荐
- redis之 Redis常用数据类型
Redis最为常用的数据类型主要有以下7种: 一. String (字符) 常用命令: set,get,decr,incr,mget 等. 应用场景:String是最常用的一种数据类型,普通的key ...
- 序章:为什么学习使用kotlin、及kotlin的一些碎碎念
为什么使用kotlin? 当然是因为项目目前的开发语言是kotlin啊! 一方面是想能够尽快适应项目,另一方面,kotlin这门语言独特的语法,确实很吸引我,也让我意识到java代码在某些程度上的繁琐 ...
- .NET Framework、C#、CLR和Visual Studo之间的版本关系
.NET Framework.C#.CLR和Visual Studo之间的版本关系 参考 .NET Framework.C#.CLR和Visual Studo之间的版本关系
- WPF中ToolTip的自定义
ToolTip或者PopUp这个控件在做界面时会经常用到.如何对ToolTip进行自定义呢? 1.首先自定义tooltip的controlTemplate,完全清除系统默认效果, 如下: ...
- [MySQL-MM] 生产环境自动恢复MM中一台M2库的过程,分享从零开始写的自动化重建脚本以及思路 (转)
必须是MM架构,而且一台主库M1是完好无损的,一台主库M2可以根据M1主库来进行重建:如果MS架构,自己可以稍微做一下脚本修改动作,也能使用,架构如下图所示: 3 总体思路,建立主脚本a_build ...
- spring 学习二 @RequestMapping
RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径. RequestMapping注解有六个属性,下面我们把她 ...
- iOS离屏渲染
为什么会使用离屏渲染 当使用圆角,阴影,遮罩的时候,图层属性的混合体被指定为在未预合成之前不能直接在屏幕中绘制,所以就需要屏幕外渲染被唤起. 屏幕外渲染并不意味着软件绘制,但是它意味着图层必须在被显示 ...
- Appium—python_ 安卓手机划屏幕操作
开始的时候 不知道 python_unittest框架的命名规则,导致方法进不去,后来 改变方法名 能获取 # conding=utf- from appium import webdriver im ...
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 85
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 85;import sys reload(sys) sys.s ...
- log4j配置文件加载方式
使用背景: apache的log4j是一个功能强大的日志文件,当我们使用eclipse等IDE在项目中配置log4j的时候,需要知道我们的配置文件的加载方式以及如何被加载的. 加载方式: (1).自动 ...