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

推荐订阅源

V
Visual Studio Blog
Martin Fowler
Martin Fowler
aimingoo的专栏
aimingoo的专栏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
S
Securelist
博客园 - Franky
P
Proofpoint News Feed
量子位
雷峰网
雷峰网
Security Latest
Security Latest
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Latest news
Latest news
L
Lohrmann on Cybersecurity
W
WeLiveSecurity
月光博客
月光博客
Hacker News: Ask HN
Hacker News: Ask HN
宝玉的分享
宝玉的分享
GbyAI
GbyAI
小众软件
小众软件
M
MIT News - Artificial intelligence
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Secure Thoughts
The Cloudflare Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
IT之家
IT之家
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Threat Research - Cisco Blogs
Stack Overflow Blog
Stack Overflow Blog
有赞技术团队
有赞技术团队
Attack and Defense Labs
Attack and Defense Labs
Y
Y Combinator Blog
Scott Helme
Scott Helme
O
OpenAI News
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
V
Vulnerabilities – Threatpost
博客园 - 【当耐特】
K
Kaspersky official blog
Microsoft Azure Blog
Microsoft Azure Blog
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
腾讯CDC
NISL@THU
NISL@THU
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Tailwind CSS Blog

博客园 - 灌木大叔

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("您取消了本次上传.");

}