将包含内容和元数据的文件上传到谷歌文档会返回400无效/错误请求
Uploading file with content and metadata to Google Docs returns 400 Invalid/Bad Request
我正试图从HTML5文件对话框上传一个文件到我的谷歌文档帐户。
我正在使用Google文档列表API 3.0版本来处理此请求,以及此链接中的说明。
查询后,我从DocumentsListJSON中收集正确的可恢复创建链接URL,并将XML实体POST到该链接以获得位置网址。我得到了200 OK的回复和位置URL。
在设置了头之后,我向位置URL发出PUT请求以上传文件。出于这个问题的目的,该文件小于512kb,不需要分块。
提交这个PUT请求后,我收到了一个400错误请求(有时是400无效请求)响应,并且文件没有上传。
有趣的是,如果我省略Content-Range标头,我可以上传一个小文件,但文本文件以外的文件已损坏。
我已经对加载在内存中的文件和本地文件进行了二进制比较。看起来他们很匹配。
为了访问API,我在一个带有http://www.google.com/jsapi.
我使用gadgets.io.makeRequest获取、POST和PUT数据。考虑到我能够毫无问题地查询文档列表,我的授权头和令牌似乎是正确的。
我应该采取什么措施来解决这个问题?
样本输出
PUT /feeds/upload/create-session/default/private/full?v=3.0&convert=false&upload_id=[id]
Host: docs.google.com
X-Shindig-AuthType: oauth
Authorization: OAuth oauth_body_hash="x", opensocial_owner_id="x", opensocial_viewer_id="x", opensocial_app_id="x", opensocial_app_url="x", xoauth_signature_publickey="x", xoauth_public_key="x", oauth_version="1.0", oauth_timestamp="x", oauth_nonce="x", opensocial_container="x", oauth_token="x", oauth_consumer_key="x", oauth_signature_method="RSA-SHA1", oauth_signature="x"
Content-Length: 433
Content-Range: bytes 0-433/433
Content-Type: application/x-javascript
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79 Safari/535.11,gzip(gfe)
X-Forwarded-For: 74.203.139.139
X-shindig-dos: on
X-Upload-Content-Length: 433
X-Upload-Content-Type: application/x-javascript
[bytes 0-433]
==== Received response 1:
HTTP/1.1 400
Cache-Control: no-cache, no-store, must-revalidate
Content-Length: 15
Content-Type: text/html; charset=UTF-8
Date: Thu, 05 Apr 2012 19:49:48 GMT
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Pragma: no-cache
Server: HTTP Upload Server Built on Apr 2 2012 11:47:06 (1333392426)
Via: HTTP/1.1 GWA
X-Google-Cache-Control: remote-fetch
Invalid Request
====
errors: 400 Error
编辑
二进制代码处理数据:
// -- File Upload functions --
function constructContent(docTitle) {
var atom = ['<?xml version='"1.0'" encoding='"UTF-8"?>',
'<entry xmlns='"http://www.w3.org/2005/Atom'" xmlns:docs='"http://schemas.google.com/docs/2007'">',
'<title>' + docTitle + '</title>',
'</entry>'].join('');
return atom;
}
function writeFile(file, under512) {
var body = constructContent(file.name.toString());
var params = {};
console.log(body);
params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.OAUTH;
params[gadgets.io.RequestParameters.OAUTH_SERVICE_NAME] = "google";
params[gadgets.io.RequestParameters.OAUTH_USE_TOKEN] = "always";
params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
params[gadgets.io.RequestParameters.POST_DATA] = body;
params[gadgets.io.RequestParameters.HEADERS] = {
"Content-Type" : "application/atom+xml",
"Content-Length" : "359",
"X-Upload-Content-Type" : file.type.toString(),
"X-Upload-Content-Length" : file.size.toString()
};
var data;
submitRequest(resumableLink + '&convert=false', params, function(requestSuceeded, data) {
if ( data.rc == 200 ) {
console.log(data);
console.log(data.headers.location[0].toString());
if ( under512 == true ) {
continueFile(data.headers.location[0].toString(), file, ('0-' + file.size + '/' + file.size).toString(), under512, params);
}
else {
continueFile(data.headers.location[0].toString(), file, ('0-524287/' + file.size).toString(), under512, params);
}
}
});
}
// recursive
function continueFile(location, file, contentRange, under512) {
console.log('location: ' + location);
var contentLength = "0";
var reader = new FileReader();
var blob;
var start = contentRange.split('-')[0].toString();
var stop = contentRange.split('-')[1].split('/')[0].toString();
console.log(file.size);
console.log(file.type);
console.log('start: ' + start);
console.log('stop: ' + stop);
console.log(("bytes " + contentRange).toString());
console.log(contentRange);
( under512 == true ) ? contentLength = contentRange.split('/')[1].toString() : contentLength = "524288";
if ( file.webkitSlice ) {
blob = file.webkitSlice(start, stop+1);
}
else {
blob = file.mozSlice(start, stop+1);
}
reader.readAsBinaryString(blob);
reader.onloadend = function(evt) {
if ( evt.target.readyState == FileReader.DONE ) {
console.log(evt.target.result);
var b = showResult(reader);
// console.log('binary: ' + b);
var params = {};
params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.OAUTH;
params[gadgets.io.RequestParameters.OAUTH_SERVICE_NAME] = "google";
params[gadgets.io.RequestParameters.OAUTH_USE_TOKEN] = "always";
params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.PUT;
params[gadgets.io.RequestParameters.POST_DATA] = evt.target.result;
params[gadgets.io.RequestParameters.HEADERS] = {
"Content-Length" : (contentLength).toString(),
"Content-Type" : file.type.toString(),
"Content-Range" : ("bytes " + contentRange).toString().trim()
// "GData-Version" : "3.0"
};
submitRequest(location.toString().trim(), params, function(requestSucceeded, data) {
if ( data.rc == 308 ) {
var newStart = start + 524288;
var newEnd;
( end + 524288 > file.size ) ? newEnd = file.size : newEnd = end + 524288;
var range = (newStart + '-' + newEnd + '/' + file.size).toString().trim();
continueFile(data.headers.location.toString().trim(), file, range, under512);
}
else if ( data.rc == 201 ) {
console.log('done!');
}
else {
console.log('terrible error.');
console.log(data);
writeObj(data);
}
});
}
};
}
function uploadFile() {
var file = document.getElementById('uploads').files[0];
var under512 = false;
( file.size < 524287 ) ? under512 = true : under512 = false;
writeFile(file, under512);
}
如文档中所述,延续请求没有X-Upload-Content-*头,看起来应该更像:
PUT [next location]
Content-Length: 524288
Content-Type: application/pdf
Content-Range: bytes 0-524287/1073741824
此外,在您的日志中,0-433是434字节,而不是433字节。
相关文章:
- MongoDB (php) - 以数组而不是多个属性的形式返回文档属性
- 返回一个关联数组,而不是mongoose中的文档数组
- 返回午夜后创建的文档
- 为什么返回文档.getElementsByClassName正在工作,而getElementbyId不在Watir中
- 仅返回嵌入文档列表中的某些字段
- 通过 Meteor / Mongo 返回子文档数组
- 为什么即使我已经订阅了该集合(Meteor),文档仍返回 null
- 从 Meteor 中的集合文档返回单个值
- 获取 0 的文档元素返回什么
- 将包含内容和元数据的文件上传到谷歌文档会返回400无效/错误请求
- 返回一组文档,其中所有子数组都包含环回或nodejs中的给定值
- scrollTop为javascript中的所有文档元素返回0,但滚动事件在body标记上触发
- javascript文档.getElementById('').value返回null
- 文档's总高度函数's的返回值在浏览器刷新时发生更改(F5)
- AngularJS文档:工厂函数没有返回任何内容
- 如何在Mongodb中返回符合给定条件的子文档数组
- 如果不存在则创建文档,否则,更新-无论哪种情况都返回文档
- 为什么在Mongoose中查询子文档时没有返回文档
- $(window).height()返回文档高度(但声明了doctype)
- 我可以返回文档的值吗?setcookie中的Getelementbyid