因为项目需要,结合之前的项目使用的Uploadifive上传做了点小小的拓展。

var YSHT = (function ($, ysht) {ysht.UploadFiles = function (options) {var defaults = {element: "input[type='file']",auto: true,             //是否自动上传buttonText: "附件上传", //定义显示在按钮上的文本fileSizeLimit: "20GB",//上传文件大小限制uploadLimit: 999,       //上传文件个数width: 100,height:34,formData: {},callback: null,multi: false,url: "",removeCompleted:true};var opts = $.extend({}, defaults, options);if (!opts.url) {alert("初始化上传控件发生错误!");return false;}if (!$(opts.element).attr("id")) {alert("初始化上传控件发生错误!");return false;}var files = [];var uploadify = $(opts.element).uploadifive({auto: opts.auto,width: opts.width,height: opts.height,buttonClass: "btn btn-primary",buttonText: "<i class=\"fa fa-upload\"></i>&nbsp;&nbsp;" + opts.buttonText,dnd: false,           //禁用拖拽上传功能
            fileSizeLimit: opts.fileSizeLimit,fileType: true,      //允许所有的文件类型(允许所有图片'image/*') method: "post",multi: opts.multi,removeCompleted: opts.removeCompleted,//是否移除从队列张完成上传的项目
            formData: opts.formData,uploadLimit: opts.uploadLimit,uploadScript: opts.url,onError: function (res) {alert(res.errMsg);return false;},onUploadComplete: function (file, res) {res = JSON.parse(res);var html = "<div class = 'img-view'><img class = 'previewImg previewImg_1' width='200' height='200' src=''";html += "serverFilePath = '" + res.FilePath + "'";html += "fileName = '" + res.FileName + "'";html += "fileExt='" + res.FileExt + "'/>";html += "<a class='img-del' del-path='" + res.FilePath + "'";html += "filename = '" + res.FileName + "'fileext='" + res.FileExt + "'>删除</a></div>";$("#preview").append(html)var imgArray = document.getElementById("preview").getElementsByTagName("img");var finallyImg = document.getElementById("preview").getElementsByTagName("img")[imgArray.length - 1];$(finallyImg).siblings('a').attr({ 'del-path': res.FilePath, "fileName": res.FileName, fileExt: res.FileExt });var reader = new FileReader();var path = res.SavePath;reader.readAsDataURL(file);reader.onload = function (e) {url = reader.result;finallyImg.src = url;$("#resultImage")[0].src = url;};            }});//RETURN {//    uPLOADIFY: UPLOADIFY,//    fILES: FILES//};
    };return ysht;
}(jQuery, window.YSHT || {}));

后台代码分为压缩上传和不压缩上传,其中压缩上传的方法是在网上找了个大神写的。因为是改别人的代码,很多写的很乱地方没有改的完善,有空需要好好整理一下

        /// <summary>/// 未压缩上传/// </summary>/// <param name="mfile"></param>/// <returns></returns>
        [HttpPost]public ActionResult NotCompressUploadImg(HttpPostedFileBase mfile){try{if (mfile != null){string fileName = Path.GetFileName(mfile.FileName);// 原始文件名称string fileExtension = Path.GetExtension(fileName); // 文件扩展名string saveName = Guid.NewGuid() + fileExtension; // 保存文件名称 这是个好方法。string savepath = Server.MapPath("../ImageTemp/") + fileName;mfile.SaveAs(savepath);if (IsPicture(savepath) == 1){return Json(new { Success = false, Message = "该图发现木马文件!" }, JsonRequestBehavior.AllowGet);}else{var path = "/Upload/Image/";var uploadpath = Server.MapPath("~" + path);if (!Directory.Exists(uploadpath)){Directory.CreateDirectory(uploadpath);}//string saveName = Encrypt.GenerateOrderNumber() + fileExtension; // 保存文件名称 这是个好方法。mfile.SaveAs(uploadpath + saveName);var fullPath = uploadpath.Replace("\\", "/") + saveName;return Json(new { Success = true, FilePath = path + saveName, FileName = fileName, FileExt = fileExtension, SavePath = fullPath, Message = "上传成功!" });}}return Json(new { Success = false, Message = "请选择要上传的文件!" }, JsonRequestBehavior.AllowGet);}catch (Exception ex){return Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet);}}

public ActionResult CompressImgBase64Uploadfinal(HttpPostedFileBase mfile){try{if (mfile != null){//先保存到临时文件夹,如果是病毒,就删除sstring fileName = Path.GetFileName(mfile.FileName);// 原始文件名称string fileExtension = Path.GetExtension(fileName); // 文件扩展名string saveName = Guid.NewGuid() + fileExtension; // 保存文件名称 这是个好方法。string savepath = Server.MapPath("../ImageTemp/") + fileName;mfile.SaveAs(savepath);var readpath = savepath.Replace("\\", "/");System.Drawing.Image img = new System.Drawing.Bitmap(readpath);if (IsPicture(savepath) == 1){return Json(new { Success = false, FilePath = "", FileName = "", FileExt = ".jpg", Message = "上传的图片有病毒!" });}var path = "/Upload/Image/";var uploadpath = Server.MapPath("~") + path;if (!Directory.Exists(uploadpath)){Directory.CreateDirectory(uploadpath);}string savepath1 = uploadpath + saveName;//图片压缩上传bool isSuccess = ReduceImage(img, savepath1, 50);if (isSuccess){System.IO.File.Delete(savepath);return Json(new { Success = true, FilePath = path + saveName, FileName = fileName, FileExt = ".jpg", Message = "上传成功!" });}}return Json(new { Success = false, FilePath = "", FileName = "", FileExt = "", Message = "上传失败!" }); ;}catch (Exception ex){return Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet);}}

/// <summary>/// 压缩图片并保存/// </summary>/// <param name="iSource">图片文件</param>/// <param name="outPath">文件保存路径</param> 例如 var outPath = Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(file.FileName));/// <param name="flag">值越大代表图片质量越好,一般默认控制在50为最佳压缩质量</param>/// <returns></returns>public static bool ReduceImage(Image iSource, string outPath, int flag){ImageFormat tFormat = iSource.RawFormat;EncoderParameters ep = new EncoderParameters();long[] qy = new long[1];qy[0] = flag;EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);ep.Param[0] = eParam;try{ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageDecoders();ImageCodecInfo jpegICIinfo = null;for (int x = 0; x < arrayICI.Length; x++){if (arrayICI[x].FormatDescription.Equals("JPEG")){jpegICIinfo = arrayICI[x];break;}}if (jpegICIinfo != null)iSource.Save(outPath, jpegICIinfo, ep);elseiSource.Save(outPath, tFormat);return true;}catch{return false;}finally{iSource.Dispose();}}

转载于:https://www.cnblogs.com/caiyi168/p/9364925.html

Uploadifive上传相关推荐

  1. Uploadify/uploadifive上传(中文文档)

    Uploadify 是一款基于 JQuery 的优秀的文件/图片上传的插件,有基于Flash和HTML5两种版本.     Uploadify/uploadifive主要特点有:   1. 多文件上传 ...

  2. uploadifive上传,无需flash插件案列

    前端: $('#updateFiles').uploadifive({'auto': true, //是否自动上传,默认true'multi': true, //是否多文件'uploadScript' ...

  3. Uploadifive 上传'fileType'格式如何限制?

    Uploadifive: 'fileType':[ "application/pdf", "application/msword", "applica ...

  4. Spring mvc,uploadifive 文件上传实践(转自:https://segmentfault.com/a/1190000004503262)

     1.前台页面: 引入js和css 全选复制放进笔记 <link type="text/css" rel="stylesheet" href=&quo ...

  5. uploadifive struts2实现图片上传

    uploadify是来自国外的一款优秀的jQuery上传插件,主要功能是批量上传文件,带进度显示. 下面以HTML5 Version版本为例介绍uploadifive的使用,结合Struts2 各软件 ...

  6. jquery uploadify 多文件上传插件 使用经验

    2019独角兽企业重金招聘Python工程师标准>>> Uploadify 官网:http://www.uploadify.com/ 一.如何使用呢? 官网原文:http://www ...

  7. uploadify---jQuery多文件上传

    介绍 ploadify是JQuery的一个上传插件,支持多文件上传,实现的效果非常不错,带进度显示. 下载 前往官网进行下载,但小编发现,各路人马提供的官网全都打不开,在Github下载的文件为upl ...

  8. Bootstrap框架----单张图片上传实现---Uploadify插件

    框架搭建 我们在之前的文章中已经实现了 单张图片的上传,使用的是最基本的 File类型的input提交的方式,上传到远程静态资源器和七牛云. 之前的文章关注的地方 主要是后端,也就是图片上传保存到哪里 ...

  9. jquery 文件上传插件_10个jQuery文件上传插件

    jquery 文件上传插件 这篇热门文章于2016年7月更新,以反映文件上传插件的当前状态. 有关旧文章的评论已删除. 使用Ajax实施文件上传可能非常困难,特别是如果您需要拖放支持,图像预览或进度条 ...

  10. 解决IE8下上传文件问题,无需flash,无需html5

    解决IE8下上传文件问题 场景:实现上传文件功能,重写上传按钮样式,并需要得到后台返回的数据来动态修改页面. 要求:不需要flash以及其他额外需要的东西. 尝试过的方法:1.webuploader. ...

最新文章

  1. windows7下硬盘安装32位ubuntu12.04LTS
  2. 24.command-executor
  3. 数据库监控框架 oneproxy-monitor 开源了
  4. ipad和iphone切图_如何从iPhone和iPad上的Mail应用程序删除电子邮件帐户
  5. hysys动态模拟教程_Steam特别好评!新游《模拟消防英豪》现已上市
  6. 未处理sqlexception中value_类 java.sql.SQLException 的使用 (Java Platform SE 6)
  7. Java程序设计教程(第3版)雍俊海 全书例程-1
  8. 微信H5 分享(自定义标题、图片、链接) 与 禁止分享(微信隐藏功能按钮) 功能的写法
  9. C++实现端口扫描器
  10. SSL/TLS 受诫礼攻击漏洞的问题的解决记录
  11. Java 的package 機制 王森
  12. css中英文单词换行的问题
  13. 利用vuex关闭当前tab页签
  14. 读书笔记-哈佛大学极简经济学2
  15. 纯CSS3实现的纸张褶起动效
  16. 线性规划之二 —— 单纯形算法(详解)
  17. 【学习】线程池:获取任务执行结果get与取消任务cancel方法
  18. Windows下VMware准备IOS开发环境
  19. Android之扫描二维码和根据输入信息生成名片二维码
  20. LQ0052 冰雹数【枚举+角谷猜想】

热门文章

  1. 论文阅读-AKS_CoRR_2011
  2. 数字疗法001 | 心理疾病太痛苦。把你的心理健康交给昭阳医生吧
  3. 氧化镁MgO晶体基片|钛酸锶SrTiO3晶体基片|铌酸锂LiNbO3晶体基片;直径10mm
  4. Eclipse WTP
  5. IOS中impactor报81错误解决方法
  6. cadence allegro - 四层板设置 ……F
  7. element-ui的Progress进度条,当前状态样色和背景色的设置
  8. python字符串中常见的内置方法
  9. 四、Storm入门之Spout
  10. 屌丝码农该怎么过周末