多个文件上传得到重复和重复

编程入门 行业动态 更新时间:2024-10-25 16:25:49
本文介绍了多个文件上传得到重复和重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用HTTP post功能上传多个文件,但是当我选择4个以上的文件后,上传后的文件会被重复并在上传的文件夹中重复,例如如果我选择了9个文件,那么它将显示32个文件或一段时间18个文件我怎么能解决它任何可以帮助我的人?这是我的代码 我尝试过:

i am using HTTP post function to upload multiple files but when i select more than 4 files then after uploading the files are duplicated and repeated in uploaded folder for example if i select 9 files then it will appear 32 files or some time 18 files how can i solve it any person who can help me please ?? here is my code What I have tried:

using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace FileUploading.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } [HttpPost] public JsonResult Upload() { try { HttpFileCollectionBase files = Request.Files; for (int i = 0; i < files.Count; i++) { var newFile = files[i]; string time = DateTime.Now.ToString().Replace("/", "_"); time = time.Replace(":", "_"); string path = Server.MapPath("~/Images/" + time + "_" + newFile.FileName); newFile.SaveAs(path); } return Json("Uploaded", JsonRequestBehavior.AllowGet); } catch { return Json("Failed", JsonRequestBehavior.AllowGet); } } } }

及以下是索引视图代码我在上传时也应用多个进度条

and below is index view code i also apply multiple progressbar on uploading

@{ ViewBag.Title = "Index"; } <h2>Index</h2> @using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { <input type="file" name="file" multiple id="file"/><br /> <div id="all-progress-bars"></div> <span id="display"></span> <button class="btn alert-success" href="javascript:void(0)" id="abc">Upload</button> } <script type="text/javascript"> $("#abc").click(function (e) { e.preventDefault(); var totalCountOfFiles = document.getElementById("file").files.length; var formDataToPost = new FormData(); var str = ''; for (var i = 0; i < totalCountOfFiles; i++) { str += '<div class="progress"><div class="progress-bar pbbb' + i + ' progress-bar-striped active" id="progressBar" role="progressbar" aria-valuenow="45" aria-valuemin="0" aria-valuemax="100" style="width: 0%">File ' + (i+1) + '<span class="sr-only"></span></div></div>'; } $('#all-progress-bars').html(str); var counter = 1; var percentComplete = 0; for (var i = 0; i < totalCountOfFiles; i++) { var file = document.getElementById("file").files[i]; formDataToPost.append("file", file); $.ajax({ xhr: function () { var xhr = new window.XMLHttpRequest(); //Upload progress xhr.upload.addEventListener("progress", function (evt) { if (evt.lengthComputable) { //alert(counter); percentComplete = evt.loaded / evt.total; $('#all-progress-bars').children('.progress:nth-child('+counter+')').children('div').css('width', percentComplete * 100 + '%') //Do something with upload progress console.log(percentComplete); } }, false); //Download progress xhr.addEventListener("progress", function (evt) { if (evt.lengthComputable) { var percentComplete = evt.loaded / evt.total; //Do something with download progress console.log(percentComplete); } }, false); return xhr; }, type: 'POST', url: "@Url.Action("Upload", "Home")", data: formDataToPost, dataType: 'json', contentType: false, processData:false, success: function (data) { $('#all-progress-bars').children('.progress:nth-child(' + counter + ')').children('div').css('width', '100%') counter++; } }); } }); </script>

推荐答案

( #abc)。click(function(e){ e.preventDefault(); var totalCountOfFiles = document .getElementById( file)。files.length; var formDataToPost = new FormData(); var str = ' '; for ( var i = 0 ; i < totalCountOfFiles; i ++){ str + = ' < div class =progress>< div class =progress-bar pbbb' + i + ' progress-bar-striped activeid =progressBarrole =progressbararia-valuenow =45aria-valuemin =0aria-valuemax = 100style =width:0%>文件' +(i + 1)+ ' < span class =sr-only>< / span>< / div>< / div>'; } ("#abc").click(function (e) { e.preventDefault(); var totalCountOfFiles = document.getElementById("file").files.length; var formDataToPost = new FormData(); var str = ''; for (var i = 0; i < totalCountOfFiles; i++) { str += '<div class="progress"><div class="progress-bar pbbb' + i + ' progress-bar-striped active" id="progressBar" role="progressbar" aria-valuenow="45" aria-valuemin="0" aria-valuemax="100" style="width: 0%">File ' + (i+1) + '<span class="sr-only"></span></div></div>'; }

(' #all-progress-bars').html(str); var counter = 1 ; var percentComplete = 0 ; for ( var i = 0 ; i < totalCountOfFiles; i ++){ var file = document.getElementById( file)。files [i]; formDataToPost.append( file,file); ('#all-progress-bars').html(str); var counter = 1; var percentComplete = 0; for (var i = 0; i < totalCountOfFiles; i++) { var file = document.getElementById("file").files[i]; formDataToPost.append("file", file);

.ajax({ xhr:function(){ var xhr = new window.XMLHttpRequest(); // 上传进度 xhr.upload.addEventListener( progress,function(evt){ if (evt.lengthComputable){ // alert( counter); percentComplete = evt.loaded / evt.total; .ajax({ xhr: function () { var xhr = new window.XMLHttpRequest(); //Upload progress xhr.upload.addEventListener("progress", function (evt) { if (evt.lengthComputable) { //alert(counter); percentComplete = evt.loaded / evt.total;

更多推荐

多个文件上传得到重复和重复

本文发布于:2023-11-22 21:01:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619006.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   文件上传

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!