C#上传图片(含有图片大小格式过滤以及改变像素安全存储)
示例一:
public JsonResult Upload(string parameter)
{
var file = Request.Files[];
try
{
//LogHelper.Info("文件长度:" + file.ContentLength.ToString() + "||最大照片:" + 1024 * 1024 * (Config.UploadFileSizeLimitInMB) + "||" + Config.UploadFileSizeLimitInMB);
if (file == null || file.ContentLength == )
{
throw new ValidationException("无上传文件");
}
if (file.ContentLength > * * Convert.ToInt32(uploadFileSize))
{
throw new ValidationException("上传文件过大");
}
#region checkfile
string fType = file.ContentType;//获取图像的类型 bool isimage = fType == "image/bmp" || fType == "image/gif" || fType == "image/pjpeg" || fType == "image/jpeg" || fType == "image/x-png" || fType == "image/png"; if (fType == "application/octet-stream")
{
BinaryReader reader = new BinaryReader(file.InputStream);
string fileClass;
byte buffer;
buffer = reader.ReadByte();
fileClass = buffer.ToString();
buffer = reader.ReadByte();
fileClass += buffer.ToString();
//reader.Close();
if (!(fileClass == "" || fileClass == "" || fileClass == "" || fileClass == ""))
//255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
{
throw new ValidationException("请上传图片格式的文件");
}
else
{
isimage = true;
} file.InputStream.Position = ;
} string fext = Path.GetExtension(file.FileName).ToLower(); //获取文件后缀 bool fextIsImage = fext == ".bmp" || fext == ".gif" || fext == ".png" || fext == ".jpeg" || fext == ".jpe" || fext == ".jpg"; if (!(isimage && fextIsImage))
{
throw new ValidationException("请上传图片格式的文件");
}
#endregion string uploadPath = string.Format("{0}\\{1}", uploadFilePath, SqlTimeHelper.GetTime().ToString("yyyyMMdd"));
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
var fileId = Guid.NewGuid(); System.Drawing.Image sImage = System.Drawing.Image.FromStream(file.InputStream);
int tw = sImage.Width - ;
int th = sImage.Height - ;
WatermarkHelper.AddWartermark(sImage); ImagesHelper.DrawImage(sImage, string.Format("{0}\\{1}{2}", uploadPath, fileId, fext), tw, ); ////原始图片的宽度和高度
//System.Drawing.Bitmap objPic, objNewPic; //图像对象
//objPic = new System.Drawing.Bitmap(sImage);
//objNewPic = new System.Drawing.Bitmap(objPic, tw, th); //使用指定的大小初始化objNewPic
//objNewPic.Save(string.Format("{0}\\{1}{2}", uploadPath, fileId, fext));
//objPic.Dispose();
//objNewPic.Dispose();
sImage.Dispose(); //释放资源 var rtnJson = Resource.SaveFile(file.FileName, fileId, fext); //下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失
return Json(new ApiSuccessResponse<FileUploadModel>(rtnJson));
}
catch (ValidationException ve)
{
return Json(new ApiInvalidParaExceptionResponse(ve.Message));
}
catch (Exception ue)
{
return Json(new ApiExceptionResponse<string>(ue));
}
}
示例二:
public bool SaveStringToFile(string token, string fileName, string content)
{
try
{
string fext = Path.GetExtension(fileName).ToLower(); //获取文件后缀
bool fextIsImage = fext == ".bmp" || fext == ".gif" || fext == ".png" || fext == ".jpeg" || fext == ".jpe" || fext == ".jpg"; byte[] filecontent = System.Convert.FromBase64String(content);
string path = System.Configuration.ConfigurationSettings.AppSettings["UploadFilePath"];
BinaryReader reader = new BinaryReader(new MemoryStream(filecontent));
string fileClass;
byte buffer;
buffer = reader.ReadByte();
fileClass = buffer.ToString();
buffer = reader.ReadByte();
fileClass += buffer.ToString();
reader.Close();
bool isimage = false;
if (!(fileClass == "" || fileClass == "" || fileClass == "" || fileClass == ""))
//255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
{
return false;
}
else
{
isimage = true;
} if (!(isimage && fextIsImage))
{
return false;
}
path = Path.Combine(path, fileName);
FileInfo info = new FileInfo(path);
if (!Directory.Exists(info.DirectoryName))
Directory.CreateDirectory(info.DirectoryName);
if (System.IO.File.Exists(path)) System.IO.File.Delete(path);
System.IO.FileStream outfile = System.IO.File.OpenWrite(path);
outfile.Write(filecontent, , filecontent.Length);
outfile.Close();
return true;
}
catch
{
return false;
}
}
C#上传图片(含有图片大小格式过滤以及改变像素安全存储)的更多相关文章
- C#.Net 上传图片,限制图片大小,检查类型完整版
C#.Net 上传图片,限制图片大小,检查类型完整版 源代码: 处理图片类,如检查图片大小,按宽度比例缩小图片 public class CImageLibrary{ public enum Va ...
- jquery实现上传图片及图片大小验证、图片预览效果代码
jquery实现上传图片及图片大小验证.图片预览效果代码 jquery实现上传图片及图片大小验证.图片预览效果代码 上传图片验证 */ function submit_upload_picture() ...
- 关于PHP HTML <input type="file" name="img"/>上传图片,图片大小,宽高,后缀名。
在我们的系统中,不免要上传图片,视频等文件,在上传中,需要做的一些判断,文件大小等方面. 注意: 在php.ini 中的post_max_size,upload_max_filesize默认为2M,在 ...
- 在ASP.NET MVC下实现单个图片上传, 客户端服务端双重限制图片大小和格式, 服务端裁剪图片
在"MVC文件图片ajax上传轻量级解决方案,使用客户端JSAjaxFileUploader插件01-单文件上传"一文中,使用JSAjaxFileUploader这款插件实现了单文 ...
- js 上传图片,用户自定义截取图片大小
js 上传图片,用户自定义截取图片大小 js 组件
- php用jquery-ajax上传多张图片限制图片大小
php用jquery-ajax上传多张图片限制图片大小 /** * 上传图片,默认大小限制为3M * @param String $fileInputName * @param number $siz ...
- SpringMVC 图片上传,检查图片大小
使用SpringMVC+Spring 前端提交图片文件到Controller,检查上传图片大小是否符合要求 直接上代码了 1.校验图片大小 这里提供出验证的方法,用于在需要校验的地方调用 /** * ...
- 图片处理:html文本获取图片Url,判断图片大小,存数据库
1.从html文本获取图片Url /** * html文本中取出url链接 */ public class Url { public static void main(String[] args) { ...
- markdown 插入图片太大?怎么设定图片大小?
你一定在插入图片的时候,遇到图片太大,影响观感的问题. Markdown中,图片大小的设定方式有两种 第一种: 
web service顾名思义这是一种提供service的形式,而且只能通过http(web)来提供service(web service三要素:SOAP.WSDL(WebServicesDescri ...
- stacking算法原理及代码
stacking算法原理 1:对于Model1,将训练集D分为k份,对于每一份,用剩余数据集训练模型,然后预测出这一份的结果 2:重复上面步骤,直到每一份都预测出来.得到次级模型的训练集 3:得到k份 ...
- 选择提供器 - 选择监听器(selection provider-selection listener)模式
- Linux_Centos7_设置MySql定时备份
一.查看是否安装了必要的组件 [root@localhost /]# rpm -qa | grep cron cronie-1.4.11-14.el7_2.1.x86_64 cronie-anacro ...
- Java 8新特性之lambda(八恶人-2)
Major Marquis Warren 沃伦·马奎斯少校 “Tring to get a couple of bounties in to Red Rock.”我想带几个通缉犯去红石镇 一.基本介绍 ...
- sqlplus执行startup出现ORA-00119,ORA-00132错误
安装好了oracle后,执行如下操作: 执行startup后出现如下错误: ORA-00119: invalid specification for system parameter LOCAL_LI ...