swfUpload 上传图片

Posted zslm___

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 上传图片的主要内容,如果未能解决你的问题,请参考以下文章

Extjs swfUpload 多图片上传

一个Ext2+SWFUpload做的图片上传对话框

Asp.Net上传大文件带进度条swfupload

php中上传多张图片,如何解决?

基于HTML5的可预览多图片Ajax上传

基于HTML5的可预览多图片Ajax上传(转)