如何在 Titanium Appcelerator 中指定二进制数据?
How do I specify data-binary in Titanium Appcelerator?
我正在尝试使用 Dropbox API 上传文件。这是来自 Dropbox 的 documentation:
curl -X POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer <get access token>" \
--header "Dropbox-API-Arg: {\"path\": \"/Homework/math/Matrices.txt\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary @local_file.txt
我的 Appcelerator 项目中有这个:
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function(e) {
//My function
};
xhr.open('POST','https://content.dropboxapi.com/2/files/upload');
xhr.setRequestHeader('Authorization', 'My Key');
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.setRequestHeader('Dropbox-API-Arg', '{"path":"/my_path/file.txt","mode":{".tag":"add"}}');
但我不知道如何发送二进制数据参数。使用我当前的代码,我可以在我的 Dropbox 文件夹中创建一个文件,但它只是一个空文件。
从 http://docs.appcelerator.com/platform/latest/#!/api/Titanium.Network.HTTPClient 开始,您似乎只是将其传递给 xhr.send()
。您可以传递一个字符串、一个对象(经过表单编码)或一个 Titanium.Blob
.
(免责声明:我从未使用过 Appcelerator,所以这正是我阅读文档后的推测。)
我想出了一个方法来做到这一点。在我的例子中,我只需要上传一个简单的数据结构,所以我使用了一个 JSON 对象:
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function(e) {
//My function
};
xhr.open('POST','https://content.dropboxapi.com/2/files/upload');
xhr.setRequestHeader('Authorization', 'My Key');
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.setRequestHeader('Dropbox-API-Arg', '{"path":"/my_path/file.txt","mode":{".tag":"add"}}');
var my_json = {
"item1" : "content1"
};
xhr.send(JSON.stringify(my_json));
我仍然无法发送 BLOB(如 phone 图库中的图片),但如果您传递文件路径,它会起作用:
var my_path = Titanium.Filesystem.getFile(Titanium.Filesystem.tempDirectory,'my_folder');
var newFile = Titanium.Filesystem.getFile(my_path.nativePath,'file.txt');
newFile.createFile();
newFile.write('Content of my text file');
var params = {"data-binary" : newFile};
xhr.send(params);
我正在尝试使用 Dropbox API 上传文件。这是来自 Dropbox 的 documentation:
curl -X POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer <get access token>" \
--header "Dropbox-API-Arg: {\"path\": \"/Homework/math/Matrices.txt\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary @local_file.txt
我的 Appcelerator 项目中有这个:
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function(e) {
//My function
};
xhr.open('POST','https://content.dropboxapi.com/2/files/upload');
xhr.setRequestHeader('Authorization', 'My Key');
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.setRequestHeader('Dropbox-API-Arg', '{"path":"/my_path/file.txt","mode":{".tag":"add"}}');
但我不知道如何发送二进制数据参数。使用我当前的代码,我可以在我的 Dropbox 文件夹中创建一个文件,但它只是一个空文件。
从 http://docs.appcelerator.com/platform/latest/#!/api/Titanium.Network.HTTPClient 开始,您似乎只是将其传递给 xhr.send()
。您可以传递一个字符串、一个对象(经过表单编码)或一个 Titanium.Blob
.
(免责声明:我从未使用过 Appcelerator,所以这正是我阅读文档后的推测。)
我想出了一个方法来做到这一点。在我的例子中,我只需要上传一个简单的数据结构,所以我使用了一个 JSON 对象:
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function(e) {
//My function
};
xhr.open('POST','https://content.dropboxapi.com/2/files/upload');
xhr.setRequestHeader('Authorization', 'My Key');
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.setRequestHeader('Dropbox-API-Arg', '{"path":"/my_path/file.txt","mode":{".tag":"add"}}');
var my_json = {
"item1" : "content1"
};
xhr.send(JSON.stringify(my_json));
我仍然无法发送 BLOB(如 phone 图库中的图片),但如果您传递文件路径,它会起作用:
var my_path = Titanium.Filesystem.getFile(Titanium.Filesystem.tempDirectory,'my_folder');
var newFile = Titanium.Filesystem.getFile(my_path.nativePath,'file.txt');
newFile.createFile();
newFile.write('Content of my text file');
var params = {"data-binary" : newFile};
xhr.send(params);