content-type改为FormData可以了,但是把页面代码修改了一下为什么会提示:TypeError: img is null?
<script>
function ajax(i, p, f) {
var x = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
x.open(p ? 'POST' : 'GET', i, 1);
x.setRequestHeader('Content-Type', 'multipart/form-data');
x.send(p || '');
if (f) x.onreadystatechange = f;
}
var tok
en = '5715e00907ea48f82da675fcc461a0b02e0fb36f:ANNqNncObWMh9FyUXdS8I8DsSLQ=:eyJkZWFkbGluZSI6MTQ2Nzc4NjU3NywiYWN0aW9uIjoiZ2V0IiwidWlkIjoiNTY4NDgxIiwiYWlkIjoiMTIyODAyOSIsImZyb20iOiJmaWxlIn0=';
file.onchange = function() {
var file = this.files[0];
ajax('https://sm.ms/api/upload', 'Token=' + token + '&file=' + file, function() {
if (this.readyState == 4 && this.status == 200) {
responseText.innerHTML = this.responseText;
}
})
}
</script>
<input id="file" type="file" name="file">
<div id="responseText"></div>
content-type改下,用FormData
试试用FormData对象 https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData