


This article mainly introduces you to Asp.net MVC uses swupload to implement the multi-image upload function, which has certain reference value. Interested friends can refer to it
The example in this article shares the specific code for swupload to implement multiple image uploads for your reference. The specific content is as follows
1. Download WebUploader
2. Download the files in the compressed package Copy it to your own project
3. Add a reference
<!--引入Jquery--> <script src="~/Script/jquery-1.8.2.min.js"></script> <!--引入Css--> <link href="~/CSS/webuploader.css" rel="stylesheet" /> <!--引入Js--> <script src="~/Script/webuploader.js"></script>
4. Prepare a container for images and an upload button
<p id="fileList"></p> <!--这是存放图片的容器--> <p class="cp_img_jia" id="filePicker"></p> <!--这是上传按钮-->
5. Create a Web Uploader instance and listen Event
<script type="text/javascript"> 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 = $( '<p id="' + file.id + '" class="cp_img">' + '<img alt="Sample code sharing on how Asp.net MVC uses swupload to upload multiple images" >' + '<p class="cp_img_jian"></p></p>' ), $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('<span>不能预览</span>'); 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 = $('<p class="progress"><span></span></p>') .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 = $('<p class="error"></p>').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', 'block'); }); //隐藏删除按钮 $(".cp_img").live("mouseout", function () { $(this).children(".cp_img_jian").css('display', 'none'); }); //执行删除方法 $list.on("click", ".cp_img_jian", function () { var Id = $(this).parent().attr("id"); uploader.removeFile(uploader.getFile(Id,true)); $(this).parent().remove(); }); }); </script>
6 Create a new Action in the Controller to save the image and return the image path (this method is mentioned on the blog of senior eflay)
public ActionResult UpLoadProcess(string id, string name, string type, string lastModifiedDate, int size, HttpPostedFileBase file) { string filePathName = string.Empty; string localPath = Path.Combine(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(Path.Combine(localPath, filePathName)); return Json(new { jsonrpc = "2.0", id = id, filePath = "/Upload/" + filePathName }); }
This is done.
The above is the detailed content of Sample code sharing on how Asp.net MVC uses swupload to upload multiple images. For more information, please follow other related articles on the PHP Chinese website!

如何利用GitLab进行项目文档管理一、背景介绍在软件开发过程中,项目文档是非常重要的资料,不仅能够帮助开发团队了解项目的需求和设计,还能提供给测试团队和客户参考。为了方便项目文档的版本控制和团队协作,我们可以利用GitLab来进行项目文档管理。GitLab是一个基于Git的版本控制系统,除了支持代码管理,还可以管理项目文档。二、GitLab环境搭建首先,我

抖音作为一个全球知名的短视频社交平台,靠着其独特的个性化推荐算法赢得了广大用户的青睐。本文将深入研究抖音视频推荐的价值和原理,帮助读者更好地了解和充分利用这一功能。一、什么是抖音推荐视频抖音推荐视频是根据用户的兴趣和行为习惯,利用智能推荐算法为用户筛选和推送个性化视频内容。抖音平台通过分析用户的观看历史、点赞和评论行为、分享记录等数据,从庞大的视频库中精选出最符合用户口味的视频进行推荐。这种个性化推荐系统不仅提高了用户体验,也帮助用户发现更多符合其喜好的视频内容,从而增强用户黏性和留存率。在这个

随着计算机硬件的不断发展,处理器中的CPU核心不再单独增加时钟频率,而是增加核心数量。这引发了一个显而易见的问题:如何发挥这些核心的性能?一种解决方法是通过并行编程,即同时执行多个任务,以充分利用CPU核心。这就是Go语言的一个独特之处,它是一门专为并发编程而设计的语言。在本文中,我们将探讨如何利用Go语言进行并发编程。协程首先,我们需要了解

如何利用Go语言进行内存优化引言:随着计算机科学技术的不断发展,软件开发领域也在迅猛发展。而在软件开发过程中,内存优化是非常重要的一部分。随着软件规模的增大和数据量的增长,内存的使用情况将愈加关键。本文将介绍如何利用Go语言进行内存优化,包括减少内存分配、避免内存泄漏等方面的技巧。并通过具体的代码示例,帮助读者更好地理解和应用这些技巧。一、减少内存分配使用对

如何利用PHP开发一个简单的数据分页功能引言:在网站开发中,经常会遇到需要对大量数据进行分页展示的情况,这时候就需要使用到数据分页功能。本篇文章将介绍如何利用PHP开发一个简单的数据分页功能,并提供具体的代码示例。一、准备工作:在开始编写代码之前,需要先准备好一个数据库和一张数据表,用来存储需要分页展示的数据。这里以MySQL数据库为例,创建一张名为"use

如何利用PHP技术改变你的财务状况导语:PHP是一种强大的编程语言,广泛应用于开发Web应用程序。如果你熟练掌握PHP技术,可以利用它来改善你的财务状况。本文将介绍如何通过PHP编程实现一些与财务相关的功能,并提供代码示例,帮助你更好地理解和应用。一、实现财务管理系统有一个高效和准确的财务管理系统对个人和企业来说至关重要。通过PHP编程,你可以构建一个自定义

如何利用PHP技术提高你的薪资水平PHP作为一种常用的服务器端编程语言,被广泛应用于互联网开发领域。掌握PHP技术不仅能够为你的工作增添亮点,还可以帮助你提高薪资水平。本文将介绍一些利用PHP技术提升薪资的方法,并附带示例代码。提供高效的网站开发随着互联网的迅速发展,网站已成为企业宣传、销售的重要渠道。因此,具备网站开发的技能将增加你的市场价值。PHP作为一

C++是一种功能强大而灵活的编程语言,其标准库提供了广泛的功能和工具,可以帮助开发人员快速开发高效的应用程序。本文将探讨如何有效利用C++标准库,以提高代码质量和开发效率。了解C++标准库C++标准库是C++语言的核心组成部分,包含了很多功能丰富的类和函数。标准库分为两个主要部分:标准库和标准模板库(STL)。标准库提供了诸如输入输出、字符串处理、日期时间处


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
