Cet article présente principalement en détail le plug-in de téléchargement d'images H5. Basé sur zepto, il prend en charge le téléchargement multi-fichiers, la progression et l'aperçu des images. Les amis intéressés peuvent s'y référer
Basé sur zepto, prend en charge le téléchargement multi-fichiers, la progression et l'aperçu des images, pour les téléphones mobiles.(function ($) { $.extend($, { fileUpload: function (options) { var para = { multiple: true, filebutton: ".filePicker", uploadButton: null, url: "/home/MUploadImg", filebase: "mfile",//mvc后台需要对应的名称 auto: true, previewZoom: null, uploadComplete: function (res) { console.log("uploadComplete", res); }, uploadError: function (err) { console.log("uploadError", err); }, onProgress: function (percent) { // 提供给外部获取单个文件的上传进度,供外部实现上传进度效果 console.log(percent); }, }; para = $.extend(para, options); var $self = $(para.filebutton); //先加入一个file元素 var multiple = ""; // 设置多选的参数 para.multiple ? multiple = "multiple" : multiple = ""; $self.css('position', 'relative'); $self.append('<input id="fileImage" style="opacity:0;position:absolute;top: 0;left: 0;width:100%;height:100%" type="file" size="30" name="fileselect[]" ' + multiple + '>'); var doms = { "fileToUpload": $self.parent().find("#fileImage"), // "thumb": $self.find(".thumb"), // "progress": $self.find(".upload-progress") }; var core = { fileSelected: function () { var files = (doms.fileToUpload)[0].files; var count = files.length; console.log("共有" + count + "个文件"); for (var i = 0; i < count; i++) { var item = files[i]; console.log(item.size); if (para.auto) { core.uploadFile(item); } core.previewImage(item); } }, uploadFile: function (file) { console.log("开始上传"); var formdata = new FormData(); formdata.append(para.filebase, file);//这个名字要和mvc后台配合 var xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress", function (e) { var percentComplete = Math.round(e.loaded * 100 / e.total); para.onProgress(percentComplete.toString() + '%'); }); xhr.addEventListener("load", function (e) { para.uploadComplete(xhr.responseText); }); xhr.addEventListener("error", function (e) { para.uploadError(e); }); xhr.open("post", para.url, true); //xhr.setRequestHeader("X_FILENAME", file.name); xhr.send(formdata); }, uploadFiles: function () { var files = (doms.fileToUpload)[0].files; for (var i = 0; i < files.length; i++) { core.uploadFile(files[i]); } }, previewImage: function (file) { if (!para.previewZoom) return; var img = document.createElement("img"); img.file = file; $(para.previewZoom).append(img); // 使用FileReader方法显示图片内容 var reader = new FileReader(); reader.onload = (function (aImg) { return function (e) { aImg.src = e.target.result; }; })(img); reader.readAsDataURL(file); } } doms.fileToUpload.on("change", function () { //doms.progress.find("span").width("0"); console.log("选中了文件"); core.fileSelected(); }); console.log("初始化!"); //绑定事件 $(document).on("click", para.filebutton, function () { console.log("clicked"); //doms.fileToUpload.click(); }); if (para.uploadButton) { $(document).on("click", para.uploadButton, function () { core.uploadFiles(); }); } } }); })(Zepto);
[HttpPost] public ActionResult MUploadImg(HttpPostedFileBase mfile) { return UploadImg(mfile, "Mobile"); }
[HttpPost] public ActionResult UploadImg(HttpPostedFileBase file, string dir = "UserImg") { if (CheckImg(file, imgtypes) != "ok") return Json(new { Success = false, Message = "文件格式不对!" }, JsonRequestBehavior.AllowGet); if (file != null) { var path = "~/Content/UploadFiles/" + dir + "/"; var uploadpath = Server.MapPath(path); if (!Directory.Exists(uploadpath)) { Directory.CreateDirectory(uploadpath); } string fileName = Path.GetFileName(file.FileName);// 原始文件名称 string fileExtension = Path.GetExtension(fileName); // 文件扩展名 //string saveName = Guid.NewGuid() + fileExtension; // 保存文件名称 这是个好方法。 string saveName = Encrypt.GenerateOrderNumber() + fileExtension; // 保存文件名称 这是个好方法。 file.SaveAs(uploadpath + saveName); return Json(new { Success = true, SaveName = path + saveName }); } return Json(new { Success = false, Message = "请选择要上传的文件!" }, JsonRequestBehavior.AllowGet); }
<p class="page" id="upload"> <h2>UploadImg</h2> <p id="dd" class="filePicker">点击选择文件</p> <p id="preview"></p> </p> <script> $.fileUpload({ filebutton: "#dd", previewZoom: "#preview" }); </script>
Code de fonction H5 pour implémenter le téléchargement d'images locales et leur prévisualisation
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!