1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 在Asp.net的MVC中利用swupload实现多图片上传功能的实例详解

在Asp.net的MVC中利用swupload实现多图片上传功能的实例详解

时间:2022-02-07 21:35:37

相关推荐

在Asp.net的MVC中利用swupload实现多图片上传功能的实例详解

后端开发|C#.Net教程

swupload,,图片

后端开发-C#.Net教程

这篇文章主要为大家详细介绍了 MVC使用swupload实现多图片上传功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

enet源码,vscode复制多行快捷键,无法使用ubuntu,腾讯云 启动tomcat,爬虫加密破解,php编程题库,山西seo网络推广是干嘛的lzw

本文实例为大家分享了swupload实现多图片上传的具体代码,供大家参考,具体内容如下

asp 游戏源码下载,vscode文件自动更改,更改ubuntu 源,tomcat配置wss协议,上网爬虫,php采集淘宝商品,宁波seo专员推广产品,仿360网站源码分析,织梦淘宝客模板带手机模板lzw

1. 下载WebUploader

3合一网站源码下载,sdl.h ubuntu,tomcat百度云下载,爬虫造景房,学习php就业前景教程,武汉seo技术lzw

2. 将下载到的压缩包里面的文件复制到自己的项目中

3. 添加引用

4.准备一个放图片的容器和一个上传按钮

5.创建Web Uploader实例并监听事件

var applicationPath = window.applicationPath === "" ? "" : window.applicationPath || "../../"; $(function () { var $ = jQuery, $list = $(#fileList), // 优化retina, 在retina下这个值是2 ratio = window.devicePixelRatio || 1, // 缩略图大小 thumbnailWidth = 90 * ratio, thumbnailHeight = 90 * ratio, // Web Uploader实例 uploader; uploader = WebUploader.create({ // 选完文件后,是否自动上传。 auto: false, // swf文件路径 swf: applicationPath + /Script/Uploader.swf, // 文件接收服务端。 server: applicationPath + /Home/UpLoadProcess, // 选择文件的按钮。可选。 // 内部根据当前运行是创建,可能是input元素,也可能是flash. pick: #filePicker, //只允许选择图片 accept: { title: Images, extensions: gif,jpg,jpeg,bmp,png, mimeTypes: image/* } }); // 当有文件添加进来的时候 uploader.on(fileQueued, function (file) { var $li = $(

+\ +

), $img = $li.find(img); // $list为容器jQuery实例 $list.append($li); // 创建缩略图 // 如果为非图片文件,可以不用调用此方法。 // thumbnailWidth x thumbnailHeight 为 100 x 100 uploader.makeThumb(file, function (error, src) { if (error) {$img.replaceWith(不能预览);return; } $img.attr(src, src); }, thumbnailWidth, thumbnailHeight); }); // 文件上传过程中创建进度条实时显示。 uploader.on(uploadProgress, function (file, percentage) { var $li = $(# + file.id), $percent = $li.find(.progress span); // 避免重复创建 if (!$percent.length) { $percent = $(\).appendTo($li).find(span); } $percent.css(width, percentage * 100 + \%); }); // 文件上传成功,给item添加成功class, 用样式标记上传成功。 uploader.on(uploadSuccess, function (file, response) {$(# + file.id).addClass(upload-state-done); }); // 文件上传失败,显示上传出错。 uploader.on(uploadError, function (file) { var $li = $(# + file.id), $error = $li.find(p.error); // 避免重复创建 if (!$error.length) { $error = $(\).appendTo($li); } $error.text(上传失败); }); // 完成上传完了,成功或者失败,先删除进度条。 uploader.on(uploadComplete, function (file) { $(# + file.id).find(.progress).remove(); }); //所有文件上传完毕 uploader.on("uploadFinished", function () { //提交表单 }); //开始上传 $("#ctlBtn").click(function () { uploader.upload(); }); //显示删除按钮 $(".cp_img").live("mouseover", function () { $(this).children(".cp_img_jian").css(display, lock); }); //隐藏删除按钮 $(".cp_img").live("mouseout", function () { $(this).children(".cp_img_jian").css(display, one); }); //执行删除方法 $list.on("click", ".cp_img_jian", function () { var Id = $(this).parent().attr("id"); uploader.removeFile(uploader.getFile(Id,true)); $(this).parent().remove(); }); });

6 在Controller里新建一个Action用于保存图片并返回图片路径(这方法是 eflay 前辈博客上说的)

public ActionResult UpLoadProcess(string id, string name, string type, string lastModifiedDate, int size, HttpPostedFileBase file) { string filePathName = string.Empty; string localPath = bine(HttpRuntime.AppDomainAppPath, "Upload"); if (Request.Files.Count == 0) { return Json(new { jsonrpc = 2.0, error = new { code = 102, message = "保存失败" }, id = "id" }); } string ex = Path.GetExtension(file.FileName); filePathName = Guid.NewGuid().ToString("N") + ex; if (!System.IO.Directory.Exists(localPath)) { System.IO.Directory.CreateDirectory(localPath); } file.SaveAs(bine(localPath, filePathName)); return Json(new { jsonrpc = "2.0", id = id, filePath = "/Upload/" + filePathName }); }

这样就大功告成了。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。