我使用XMLHttpRequest发送到服务器端创建的可恢复上传URL,将基于浏览器的可恢复上传到Google的云存储中.这在禁用Web安全性时完全正常,我在开发期间执行了此操作.
但现在在现实世界中,CORS一直在制造麻烦.我也尝试过与其他浏览器(没有成功),但坚持使用chrome进行进一步测试.
注意:fake.host条目/etc/HOSTS用于欺骗chrome以避免localhost限制.然而,我们的在线测试服务器的"真实"域也是如此.
使用普通的XMLHttpRequest调用启动请求:
var xhr = this.newXMLHttpRequest();
xhr.open('PUT', url, true);
xhr.setRequestHeader('Content-Type', this.currentInputFile.type);
xhr.setRequestHeader('Content-Range', 'bytes ' + startByte + '-' + (this.currentInputFile.size - 1) + '/' + this.currentInputFile.size);
xhr.onload = function(e) {
...
};
...
if (startByte > 0) {
xhr.send(this.currentInputFile.slice(startByte));
} else {
xhr.send(this.currentInputFile);
}
Run Code Online (Sandbox Code Playgroud)
然后浏览器成功启动预检请求:
Remote Address:173.194.71.95:443
Request URL:https://www.googleapis.com/upload/storage/v1/b/my-bucket-name/o?uploadType=resumable&name=aa%20spacetestSMALL_512kb.mp4&upload_id=XXXXXXXXX
Request Method:OPTIONS
Status Code:200 OK
Run Code Online (Sandbox Code Playgroud)
请求标题:
:host:www.googleapis.com
:method:OPTIONS
:path:/upload/storage/v1/b/my-bucket-name/o?uploadType=resumable&name=aa%20spacetestSMALL_512kb.mp4&upload_id=XXXXXXXXX
:scheme:https
:version:HTTP/1.1
accept:*/*
accept-encoding:gzip,deflate
accept-language:en-US,en;q=0.8,de;q=0.6
access-control-request-headers:content-range, content-type
access-control-request-method:PUT
origin:https://fake.host
referer:https://fake.host/upload.xhtml
user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X …Run Code Online (Sandbox Code Playgroud)