场景:选择本地视频后,获取文件信息,方便上传到华为云OBS
安卓真机测试
开始使用的是js的XMLHttpRequest,报TypeError,XMLHttpRequest is not defined.搜索社区后,应该是跨域问题,建议使用plus.net.XMLHttpRequest。但仍然报错,而且没看到类似的问答。代码如下:
//获取本地视频
select_video(){
var _this = this;
uni.chooseVideo({
maxDuration:30, //拍摄视频最长拍摄时间,单位秒
count:1, //上传视频的数量
sourceType:['camera','album'],
success: (res) => {
var timestamp = Date.parse(new Date()).valueOf();
console.log(timestamp)
_this.preview_video = res.tempFilePath;
var xhr = new plus.net.XMLHttpRequest();
xhr.open('GET', res.tempFilePath, true);
xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
xhr.responseType = 'blob';
xhr.onload = function(e) {
if (this.status == 200) {
_this.video_src = this.response;
_this.video_name = timestamp + '.' + _this.video_src.type.split("/")[1];
console.log(_this.preview_video)
console.log(_this.video_src)
console.log(_this.video_name)
_this.selected = true;
_this.is_video = 1;
}
};
xhr.send();
},
})
},
真机测试时,控制台打印:
09:57:48.315 App Hide at App.vue:10
09:57:52.182 App Show at App.vue:7
09:57:52.257 [Number] 1592531873000 at pages/release/release.vue:169
09:57:52.282 ReferenceError: Can't find variable: location
10:02:52.297 App Hide at App.vue:10
初学者123 (作者)
在H5可以成功上传,在真机上不行
2020-08-14 12:49