swfUpload 上传图片
前端:
<script src="~/Scripts/swfupload/swfupload.js"></script>
<script src="~/Scripts/swfupload/swfupload.queue.js"></script>
<script src="~/Scripts/swfupload/swfupload.handlers.js"></script>
<script type="text/javascript">
$(function() {
$(".uploadImg").each(function() {
$(this).InitSWFUpload({ type: 1, btntext: "上传图片", btnwidth: 86, btnheight: 28, single: false, water: true, thumbnail: true, filesize: "2048", sendurl: "/Pics/UpLoadFile", flashurl: "../../scripts/swfupload/swfupload.swf", filetypes: "*.jpg;*.jpge;*.png;*.gif;" });
});
});
</script>
<div class="uploadImg"></div>
后台:
public class UpLoad
{
int imgmaxheight = 0;
int imgmaxwidth = 0;
int thumbnailwidth = 200;
int thumbnailheight = 200;
int imgsize = 10240;
int attachsize = 51200;
int watermarktype = 2;
int filesave = 2;
string webpath = "/";
string filepath = "upload";
string fileextension = "gif,jpg,png,bmp,rar,zip,doc,xls,txt";
string watermarktext = "ZYAN";
int watermarkposition = 9;
int watermarkfontsize = 12;
string watermarkpic = "watermark.png";
int watermarktransparency = 5;
int watermarkimgquality = 80;
string watermarkfont = "ZYAN";
/// <summary>
/// 裁剪图片并保存
/// </summary>
public bool cropSaveAs(string fileName, string newFileName, int maxWidth, int maxHeight, int cropWidth, int cropHeight, int X, int Y)
{
string fileExt = Utils.GetFileExt(fileName); //文件扩展名,不含“.”
if (!IsImage(fileExt))
{
return false;
}
string newFileDir = Utils.GetMapPath(newFileName.Substring(0, newFileName.LastIndexOf(@"/") + 1));
//检查是否有该路径,没有则创建
if (!Directory.Exists(newFileDir))
{
Directory.CreateDirectory(newFileDir);
}
try
{
string fileFullPath = Utils.GetMapPath(fileName);
string toFileFullPath = Utils.GetMapPath(newFileName);
return Thumbnail.MakeThumbnailImage(fileFullPath, toFileFullPath, 180, 180, cropWidth, cropHeight, X, Y);
}
catch
{
return false;
}
}
/// <summary>
/// 文件上传方法
/// </summary>
/// <param name="postedFile">文件流</param>
/// <param name="isThumbnail">是否生成缩略图</param>
/// <param name="isWater">是否打水印</param>
/// <returns>上传后文件信息</returns>
public string fileSaveAs(HttpPostedFileBase postedFile, bool isThumbnail, bool isWater,string type)
{
try
{
string fileExt = Utils.GetFileExt(postedFile.FileName); //文件扩展名,不含“.”
int fileSize = postedFile.ContentLength; //获得文件大小,以字节为单位
string fileName = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\") + 1); //取得原文件名
string newFileName = Utils.GetRamCode() + "." + fileExt; //随机生成新的文件名
string newThumbnailFileName = "thumb_" + newFileName; //随机生成缩略图文件名
string upLoadPath = GetUpLoadPath(); //上传目录相对路径
string fullUpLoadPath = Utils.GetMapPath(upLoadPath); //上传目录的物理路径
string newFilePath = upLoadPath + newFileName; //上传后的路径
string newThumbnailPath = upLoadPath + newThumbnailFileName; //上传后的缩略图路径
//检查文件扩展名是否合法
if (!CheckFileExt(fileExt))
{
return "{\"status\": 0, \"msg\": \"不允许上传" + fileExt + "类型的文件!\"}";
}
//检查文件大小是否合法
if (!CheckFileSize(fileExt, fileSize))
{
return "{\"status\": 0, \"msg\": \"文件超过限制的大小啦!\"}";
}
//检查上传的物理路径是否存在,不存在则创建
if (!Directory.Exists(fullUpLoadPath))
{
Directory.CreateDirectory(fullUpLoadPath);
}
//保存文件
postedFile.SaveAs(fullUpLoadPath + newFileName);
//如果是图片,检查图片是否超出最大尺寸,是则裁剪
if (IsImage(fileExt) && (imgmaxheight > 0 || imgmaxwidth > 0))
{
Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newFileName,
imgmaxwidth, imgmaxheight);
}
//如果是图片,检查是否需要生成缩略图,是则生成
if (IsImage(fileExt) && isThumbnail && thumbnailwidth > 0 && thumbnailheight > 0)
{
Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newThumbnailFileName,
thumbnailwidth, thumbnailheight, "Cut");
}
//如果是图片,检查是否需要打水印
if (IsWaterMark(fileExt) && isWater)
{
switch (watermarktype)
{
case 1:
WaterMark.AddImageSignText(newFilePath, newFilePath,
watermarktext, watermarkposition,
watermarkimgquality, watermarkfont, watermarkfontsize);
break;
case 2:
WaterMark.AddImageSignPic(newFilePath, newFilePath,
watermarkpic, watermarkposition,
watermarkimgquality, watermarktransparency);
break;
}
}
//处理完毕,返回JOSN格式的文件信息
return "{\"status\": 1, \"msg\": \"上传文件成功!\", \"name\": \""
+ fileName + "\", \"path\": \"" + newFilePath + "\", \"thumb\": \""
+ newThumbnailPath + "\", \"size\": " + fileSize + ", \"type\": " + type + ", \"ext\": \"" + fileExt + "\"}";
}
catch
{
return "{\"status\": 0, \"msg\": \"上传过程中发生意外错误!\"}";
}
}
#region 私有方法
/// <summary>
/// 返回上传目录相对路径
/// </summary>
/// <param name="fileName">上传文件名</param>
private string GetUpLoadPath()
{
string path = webpath + filepath + "/"; //站点目录+上传目录
switch (filesave)
{
case 1: //按年月日每天一个文件夹
path += DateTime.Now.ToString("yyyyMMdd");
break;
default: //按年月/日存入不同的文件夹
path += DateTime.Now.ToString("yyyyMM") + "/" + DateTime.Now.ToString("dd");
break;
}
return path + "/";
}
/// <summary>
/// 是否需要打水印
/// </summary>
/// <param name="_fileExt">文件扩展名,不含“.”</param>
private bool IsWaterMark(string _fileExt)
{
//判断是否开启水印
if (watermarktype > 0)
{
//判断是否可以打水印的图片类型
ArrayList al = new ArrayList();
al.Add("bmp");
al.Add("jpeg");
al.Add("jpg");
al.Add("png");
if (al.Contains(_fileExt.ToLower()))
{
return true;
}
}
return false;
}
/// <summary>
/// 是否为图片文件
/// </summary>
/// <param name="_fileExt">文件扩展名,不含“.”</param>
private bool IsImage(string _fileExt)
{
ArrayList al = new ArrayList();
al.Add("bmp");
al.Add("jpeg");
al.Add("jpg");
al.Add("gif");
al.Add("png");
if (al.Contains(_fileExt.ToLower()))
{
return true;
}
return false;
}
/// <summary>
/// 检查是否为合法的上传文件
/// </summary>
private bool CheckFileExt(string _fileExt)
{
//检查危险文件
string[] excExt = { "asp", "aspx", "php", "jsp", "htm", "html", "js", "exe" };
for (int i = 0; i < excExt.Length; i++)
{
if (excExt[i].ToLower() == _fileExt.ToLower())
{
return false;
}
}
//检查合法文件
string[] allowExt = fileextension.Split(',');
for (int i = 0; i < allowExt.Length; i++)
{
if (allowExt[i].ToLower() == _fileExt.ToLower())
{
return true;
}
}
return false;
}
/// <summary>
/// 检查文件大小是否合法
/// </summary>
/// <param name="_fileExt">文件扩展名,不含“.”</param>
/// <param name="_fileSize">文件大小(B)</param>
private bool CheckFileSize(string _fileExt, int _fileSize)
{
//判断是否为图片文件
if (IsImage(_fileExt))
{
if (imgsize > 0 && _fileSize > imgsize * 1024)
{
return false;
}
}
else
{
if (attachsize > 0 && _fileSize > attachsize * 1024)
{
return false;
}
}
return true;
}
#endregion
}
public class PicsController
{
[HttpPost]
public string UpLoadFile(int IsWater = 0, int IsThumbnail = 0, string DelFile = "", string type = "")
{
HttpPostedFileBase _upfile = HttpContext.Request.Files["Filedata"];
bool _iswater = false; //默认不打水印
bool _isthumbnail = false; //默认不生成缩略图
if (IsWater == 1)
_iswater = true;
if (IsThumbnail == 1)
_isthumbnail = true;
if (_upfile == null)
{
return "{\"status\": 0, \"msg\": \"请选择要上传文件!\"}";
}
var upFiles = new UpLoad();
string msg = upFiles.fileSaveAs(_upfile, _isthumbnail, _iswater, type);
//删除已存在的旧文件
if (!string.IsNullOrEmpty(DelFile))
{
Utils.DeleteUpFile(DelFile);
}
return msg;
}
}
swfUpload 上传图片的更多相关文章
- swfupload上传图片
项目结构 以及插件需要的文件如图所示 前端代码: <!DOCTYPE html> <html> <head> <title>SWFUpload</ ...
- 用SWFUpload上传图片小例子
在开发项目中,经常会用到上传图片,接下来我就用一种简单的方式给大家分享一下使用SWFUpload的方式上传图片. 1.在网站根目录下新建一个SWFUpload文件夹,把下载的组建放在SWFUpload ...
- SWFUpload多图上传、C#后端跨域传文件带参数
前几天工作中用到了SWFUpload上传图片,涉及到跨域,因为前端无法实现跨域,所以只能把文件传到后端进行跨域请求,整理分享下. 效果图 前端 html部分 <!DOCTYPE html> ...
- PHP之:多图上传
撰写日期:2016-6-30 15:17:35 Thursday 参考 http://a3147972.blog.51cto.com/2366547/1381136 (08-05ThinkPHP+sw ...
- [Asp.net]Uploadify所有配置说明,常见bug问题分析
引言 之前写过一篇使用swfupload上传图片的文章:周末大放送网站图片上传,水印,预览,截图,这里分析一下,当时使用uploadify上传,无法获取上传后,图片路径的问题.当时没有测试没有成功,一 ...
- Uploadify所有配置说明,常见bug问题分析
引言 之前写过一篇使用swfupload上传图片的文章:周末大放送网站图片上传,水印,预览,截图,这里分析一下,当时使用uploadify上传,无法获取上传后,图片路径的问题.当时没有测试没有成功,一 ...
- 使用SWFUpload无刷新上传图片
使用SWFUpload组件无刷新上传图片 在做项目时,需要用到一个图片的无刷新上传,之前听说过SWFUpload,于是想要通过SWFUpload来进行图片的无刷新上传,由于我的项目属于是ASP.NET ...
- swfupload在chrome中点击上传图片按钮无反应的解决办法
chrome 22.0.XXXXX dev版上传图片按钮点击无反应原因:是GOOGLE的内建Flash PPAPI外挂所导致的. 问题原因: 由于Google浏览器(Chrome),在最新测试版22. ...
- ckeditor添加自定义按钮整合swfupload实现批量上传图片
ckeditor添加自定义按钮整合swfupload实现批量上传图片给ckeditor添加自定义按钮,由于ckeditor只能上传一张图片,如果要上传多张图片就要结合ckfinder,而ckfinde ...
随机推荐
- bindService初步了解
bindService的使用: 当需要调Service里面的方法时,可以用bindService() 首先定义一个类继承于Service,然后配置Manifest.xml文件 public class ...
- php 升级php5.5
rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm yum install php55w php55w-opcache yum install ...
- 【转】XSD (xml Schema Definition)
来自:http://www.cnblogs.com/newsouls/archive/2011/10/28/2227765.html Xml Schema的用途 1. 定义一个Xml文档中都有什么元 ...
- git --- push到远端
- python基础-内置函数详解
一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highlight=built#ascii ...
- Percona 开始尝试基于Ceph做上层感知的分布式 MySQL 集群,使用 Ceph 提供的快照,备份和 HA 功能来解决分布式数据库的底层存储问题
本文由 Ceph 中国社区 -QiYu 翻译 英文出处:Using Ceph with MySQL 欢迎加入CCTG Over the last year, the Ceph world drew m ...
- 开发错误12:gradle编译错误:Conflict with dependency com.android.support:support-annotations
在build.gradle中的configurations.all {}下添加:resolutionStrategy.force 'com.android.support:support-annota ...
- Maven 库
mvnrepository https://mvnrepository.com/ 最方便查询的库 <repositories> <repository> <id&g ...
- Echarts-画柱状,折线图
导入echarts包 <script src='../scripts/libraries/echarts/echarts-all.js'></script> 堆积图js var ...
- python求职之路
自我介绍 这是一道送分题,万年不变的第一个问题.不过有些小伙伴可能没有太在意,其实这个问题已经在面试官心中决定了你的去留意向.自我介绍的主要结构:个人基本信息 + 基本技术构成 + 项目经验(具体项目 ...