html
<input type="file" name="mp4" id="mp4" accept="video/mp4">
<button onclick="uploadvideo()"></button>
js
function uploadvideo() {
var files = document.getElementById('mp4').files; //files是文件选择框选择的文件对象数组
if(files.length == 0) return;
var form = new FormData(),
url = 'http://localhost:9527/uploadvideo', //服务器上传地址
file = files[0];
form.append('file', file);
var xhr = new XMLHttpRequest();
xhr.open("post", url, true);
//上传进度事件
xhr.upload.addEventListener("progress", function(result) {
if (result.lengthComputable) {
//上传进度
var percent = (result.loaded / result.total * 100).toFixed(2);
}
}, false);
xhr.addEventListener("readystatechange", function() {
var result = xhr;
if (result.status != 200) { //error
console.log('上传失败', result.status, result.statusText, result.response);
}
else if (result.readyState == 4) { //finished
console.log('上传成功', result);
}
});
xhr.send(form); //开始上传
}
flask by python3
@server.route('/uploadvideo', methods=['post'])
def upload_video():
upload_path = '/Users/zhangxing/Downloads/test/'
file = flask.request.files['file']
if not file:
return {"status":"fail"}
filename = file.filename
extension = filename.split('.')[-1]
print(filename)
if extension == 'mp4':
file.save(os.path.join(upload_path, filename))
result = {"status":200}
return result
else:
return {"status":"extension error"}