This article introduces to you the methods and examples of using Jquery to use the FormData attribute of HTML5 to upload files. It is very practical and friends in need can refer to it.
1. Use Jquery to use the FormData attribute of HTML5 to upload files
Before HTML5, if we needed to implement functions such as file upload servers, sometimes we had to rely on FLASH. Implementation, and after the arrival of HTML5, we can easily upload files. We only need to use a FormData attribute of HTML5, combined with Jquery, it is easy to upload files, and read the upload progress of the file. The following upload case is Based on the implementation mentioned above, I will put all the JS, CSS and HTML page code below.
Note: The FormData attribute must depend on HTML5, so if you implement the functions according to the code in this article, the browser must be upgraded to the latest (supports the HTML5 FormData attribute).
2. The HTML page code is as follows
3. The CSS code is as follows:
/*源文件头信息:Copyright(c)2014-2034 Kencery.All rights reserved. 个人博客:http://www.cnblogs.com/hanyinglong 创建人:韩迎龙(kencery) 创建时间:2015-6-24 */ body { font-family: "微软雅黑"; font-size: 12px; } .input-file { overflow: hidden; position: relative; } .input-file input { opacity: 0; filter: alpha(opacity=0); font-size: 100px; position: absolute; top: 0; right: 0; } #uploadTable { width: 500px; border-collapse: collapse; border: 1px solid Silver; }
4. The JS code is as follows:
// 源文件头信息: //// Copyright(c)2014-2034 Kencery.All rights reserved. // 创建人:韩迎龙(kencery) // 创建时间:2015-6-24 // ; (function($) { $.fn.SalesMOUNDUpload = function(options) { var defaults = { saveUrl: '', jqInput: '', maxSize: 1024 * 1024 * 100, //100M fnRemove: '', //移除文件 ,参数:文件名 fnComplete: '' //每个文件成功 ,参数:服务器端返回内容 }; var opt = $.extend(defaults, options); function getByteToM(val) { if (isNaN(val)) return val; val = val / (1024 * 1024); val = Math.round(val * 100) / 100; return val; } return this.each(function() { var $this = $(this); $this.empty(); if (typeof FormData == 'undefine') { alert('浏览器版本太低,不支持改上传!'); return; } //表头 if ($this.find('thead').length == 0) { var $thead = $(''); var $th_tr = $(''); $th_tr.append(' 文件名 '); $th_tr.append('类型 '); $th_tr.append('大小 '); $th_tr.append('状态 '); $th_tr.append('操作 '); $th_tr.appendTo($thead); $this.append($thead); } opt.jqInput[0].addEventListener('change', function(e) { var file = this.files[0]; if (!file) { return; } if (file.size > opt.maxSize) { window.alert('文件超过最大'); return; } var fd = new FormData(); var $table = $this; fd.append("uploadFile", file); var xhr = new XMLHttpRequest(); xhr.open('POST', opt.saveUrl, true); xhr.upload.addEventListener("progress", uploadProgress, false); xhr.addEventListener("load", uploadComplete, false); xhr.addEventListener("error", uploadFailed, false); xhr.addEventListener("abort", uploadCanceled, false); //表中内容 var $tr = $(''); $tr.append(' ' + file.name + ' '); $tr.append('' + file.type + ' '); $tr.append('' + getByteToM(file.size) + 'M' + ' '); $tr.append('' + 0 + ' '); $tr.append('' + '取消' + ' '); $tr.find('.upload_action a').unbind('click').bind('click', function() { xhr.abort(); }); $table.append($tr); function uploadProgress(evt) { if (evt.lengthComputable) { var percentComplete = Math.round(evt.loaded * 100 / evt.total); $tr.find('.upload_status').html(Math.round(percentComplete) + '%'); } else { $tr.find('.upload_status').html('unable to compute'); } } function uploadComplete(evt) { if (evt.target.status == 200) { $tr.find('.upload_status').html('已完成'); $tr.find('.upload_action a').html('删除'); if (typeof opt.fnComplete == 'function') { opt.fnComplete(evt.target.response); } $tr.find('.upload_action').unbind('click').bind('click', removeFile); } } function uploadFailed() { $tr.find('.upload_status').html('×'); $tr.find('.upload_status a').unbind('click').bind('click', function() { $tr.remove(); }); $tr.find('.upload_action a').html('重试'); $tr.find('.upload_action a').unbind('click').bind('click', function() { xhr.send(fd); }); } function uploadCanceled() { $tr.remove(); } function removeFile() { $tr.remove(); if (typeof opt.fnRemove == 'function') { opt.fnRemove(file.name); } } xhr.send(fd); }, false); }); }; }(jQuery));Copy after loginThe above is the detailed content of Use Jquery to implement file upload using the FormData attribute of HTML5. For more information, please follow other related articles on the PHP Chinese website!
Related labels:source:php.cnPrevious article:How to solve the problem of uploading image direction in html5 in IOS? Next article:How to make IE browser support html5 tag?Statement of this WebsiteThe content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cnLatest Articles by Author
1970-01-01 08:00:00 1970-01-01 08:00:00 1970-01-01 08:00:00 1970-01-01 08:00:00 1970-01-01 08:00:00 1970-01-01 08:00:00 1970-01-01 08:00:00 1970-01-01 08:00:00 1970-01-01 08:00:00 1970-01-01 08:00:00Latest IssuesSteps to install jQuery in Nuxt.js Although I try to add jQuery in my project, I get an error saying it's not defined. plugin...From 2023-11-06 16:41:5001225I have...From 2023-11-01 23:01:1901209Support jQuery's Transfer-Encoding:chunked I'm a web developer. In my script I use header() to set "Transfer-Encoding:chunked&qu...From 2023-10-25 23:44:3701230Automatically expand text area I'm trying to make a simple auto-expanding text area. This is my code: textarea.onkeyup=fu...From 2023-10-21 08:49:2302225Related TopicsMore>Popular RecommendationsPopular TutorialsMore>
JAVA Beginner's Video Tutorial2379834 Latest DownloadsMore>
- About us Disclaimer Sitemap
- php.cn:Public welfare online PHP training,Help PHP learners grow quickly!