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

推荐订阅源

P
Privacy & Cybersecurity Law Blog
V
V2EX
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
S
Secure Thoughts
T
Tenable Blog
Security Latest
Security Latest
The Cloudflare Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
IT之家
IT之家
Latest news
Latest news
The Hacker News
The Hacker News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
S
Security Affairs
S
Securelist
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans

博客园 - 灌木大叔

win10LTCS2021下载激活 程序员在线工具 宽带、IPTV及语音共享同一根网线传输原理 NFS与iSCSI Target性能比较 VMware vSphere安装 java解析html的table元素 chrome插件脚本background_script和content_script chrome跨域设置 Proxmox Virtual Environment(简称PVE)介绍 openwrt(LEDE)路由器简单设置 无损压缩FLAC,APE与WAV 创新的出路 走进作坊 团队--动物世界 vs2010+转换到 COFF 期间失败 windows删除用户时用户不属于此组 word无法创建工作文件,IE打开网页很慢 linux进程VIRT虚拟内存数值高 Java命令如何编译项目 UML(统一建模语言)应用 UML(统一建模语言)类关系
html5ajax上 传文件代码
灌木大叔 · 2023-03-14 · via 博客园 - 灌木大叔

html5上传文件api

使用XMLHttpRequest上传文件
var xhr = new XMLHttpRequest();

//监听选择文件信息

function fileSelected() {

//HTML5文件API操作

var file = document.getElementById('fileName').files[0];

if (file) {

var fileSize = 0;

if (file.size > 1024 * 1024)

fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';

else

fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';

document.getElementById('fileName').innerHTML = 'Name: ' + file.name;

document.getElementById('fileSize').innerHTML = 'Size: ' + fileSize;

document.getElementById('fileType').innerHTML = 'Type: ' + file.type;

}

}

//上传文件

function uploadFile() {

var fd = new FormData();

//关联表单数据,可以是自定义参数

fd.append("name", document.getElementById('name').value);

fd.append("fileName", document.getElementById('fileName').files[0]);

//监听事件

xhr.upload.addEventListener("progress", uploadProgress, false);

xhr.addEventListener("load", uploadComplete, false);

xhr.addEventListener("error", uploadFailed, false);

xhr.addEventListener("abort", uploadCanceled, false);

//发送文件和表单自定义参数

xhr.open("POST", "/user/uploadifyTest_doUpload");

xhr.send(fd);

}

//取消上传

function cancleUploadFile(){

xhr.abort();
xhr.abort();

}

//上传进度

function uploadProgress(evt) {

if (evt.lengthComputable) {

var percentComplete = Math.round(evt.loaded * 100 / evt.total);

document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%';

}

else {

document.getElementById('progressNumber').innerHTML = 'unable to compute';

}

}

//上传成功响应

function uploadComplete(evt) {

//服务断接收完文件返回的结果

alert(evt.target.responseText);

}

//上传失败

function uploadFailed(evt) {

alert("上传失败");

}

//取消上传

function uploadCanceled(evt) {

alert("您取消了本次上传.");

}