惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
有赞技术团队
有赞技术团队
博客园 - 叶小钗
博客园 - Franky
博客园_首页
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
罗磊的独立博客
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
B
Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
V
Visual Studio Blog
J
Java Code Geeks
D
Docker
Martin Fowler
Martin Fowler
Blog — PlanetScale
Blog — PlanetScale
腾讯CDC
The Register - Security
The Register - Security
Hugging Face - Blog
Hugging Face - Blog
T
Tailwind CSS Blog
The GitHub Blog
The GitHub Blog
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
博客园 - 【当耐特】
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
Google Developers Blog
S
Schneier on Security
Apple Machine Learning Research
Apple Machine Learning Research
AWS News Blog
AWS News Blog
N
Netflix TechBlog - Medium
量子位
爱范儿
爱范儿
D
Darknet – Hacking Tools, Hacker News & Cyber Security
MyScale Blog
MyScale Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Spread Privacy
Spread Privacy
V
Vulnerabilities – Threatpost
A
Arctic Wolf
C
Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
The Cloudflare Blog
C
CERT Recently Published Vulnerability Notes
L
LINUX DO - 最新话题
W
WeLiveSecurity
Hacker News: Ask HN
Hacker News: Ask HN

博客园 - today4king

DOTNET 运行AES-GCM程序 ON MACOS(错误ALGORITHM ‘AESGCM’ IS NOT SUPPORTED ON THIS PLATFORM) MACBOOK M1 PRO 下运行.NET CORE(MAC下如何与X86_64兼容) Mac上使用SQL Server作为开发用数据库 EntityFramework Core CLI Tools 几点坑 VisualStudio Code Remote 调试方法(错误Containers Docker version 17.12.0 or later required.) AWS Kubernetes/k8s kubeamd 初始化后kube-controller-manager pod CrashLoopBackOff 错误启动不了 阿里云 NAS OSS 云盘价格对比 GB/小时 kubernetes/k8s pod下多容器的设计模式(ambassador 大使代理模式,adapter 适配模式,sidecar 边车模式, init containers初始化容器) ❤️ 从125ms到11ms,记一次关键字检测过滤服务的优化 -python and Pythonnet 高效的的关键字查找和检测(哈希表和Trie前缀树和FastCheck)在实际使用中的性能 FastAPI 中的Async (并发和async/await) 阿里云vs华为云 的容器镜像服务swr使用体验 理工男对衣架的选择/衣柜收纳之衣架篇 Python中使用自定义类class作为海量数据结构时内存占用巨大的问题 关闭/开启 ubuntu 自动更新提示 python3 crypto winrandom import error Flask-Babel 中文支持(zh-CN和zh-Hans-CN) Aapache status / apache2ctl status 总是403 为什么你还在用嵌入式的方式来使用mod_wsgi?
blueImp/jQuery file upload 的正确用法(限制上传大小和文件类型)
today4king · 2016-09-27 · via 博客园 - today4king

这个插件太出名了,几乎能完成所有能想象的到的效果,包括进度条、拖拽、甚至现在已经完美支持图片视频等的处理,三个字形容就是屌爆了。最近在做上传这一部分,发现网上对于上传文件大小的限制和类型检测等的方法都不妥当,包括老外写的。blueimp提供了完整的解决方案,验证当然是有的,所以对于一个普通的上传组件来说需要下面三个组件:

<script src="//cdn.bootcss.com/blueimp-file-upload/9.12.5/js/jquery.fileupload.js"></script>
 <script src="//cdn.bootcss.com/blueimp-file-upload/9.12.5/js/jquery.fileupload-process.js"></script>
 <script src="//cdn.bootcss.com/blueimp-file-upload/9.12.5/js/jquery.fileupload-validate.js"></script>

大部分文章都没有包含process和validate这两个组件,前者负责处理上传过程中各个事件的管理,validate则是对验证的支持,如果不包含这两个组件,那么只能像下面这样来做一些验证:

add: function (e, data) {
        var uploadErrors = [];

        var acceptFileTypes = /\/(pdf|xml)$/i;
        if(data.originalFiles[0]['type'].length && !acceptFileTypes.test(data.originalFiles[0]['type'])) {
            uploadErrors.push('Tipo de Archivo no Aceptado');
        }

        console.log(data.originalFiles[0]['size']) ;
        if (data.originalFiles[0]['size'] > 5000000) {
            uploadErrors.push('Tamaño de Archivo demasiado Grande');
        }
        if(uploadErrors.length > 0) {
            alert(uploadErrors.join("\n"));
        } else {
            data.context = $('<p/>').text('Subiendo...').appendTo(document.body);
            data.submit();
        }

    }

那么用官方的方式可以如下

<pre>acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
maxFileSize: 99 * 1024 * 1024,
minFileSize: 5,
maxNumberOfFiles: 50,
messages: {
    maxFileSize: 'File exceeds maximum allowed size of 99MB',
    acceptFileTypes: 'File type not allowed'
},processfail: function (e, data) {
    var currentFile = data.files[data.index];
    if (data.files.error && currentFile.error) {
        // there was an error, do something about it
        console.log(currentFile.error);
    }
}</pre>

明显简洁很多。