无法使Image Blob JSON可序列化 [英] Unable to make an Image Blob JSON Serializable
问题描述
由于base64encode失败而创建的JIRA票证: https://jira.appcelerator.org/browse/TC-5876
JIRA Ticket created due to base64encode failure: https://jira.appcelerator.org/browse/TC-5876
我当前的CFG: Titanium SDK 5.1.2.GA 在iPhone iOS 9.1上进行测试
My Current CFG: Titanium SDK 5.1.2.GA Testing on an iPhone iOS 9.1
我陷入了一个客户项目的问题,该项目要求将在设备上(使用相机)拍摄的图像发送到WebService,然后在使用该应用程序的任何设备(Android和iOS设备)上都可以看到. Titanium在拍照后(不是JSON可序列化的)提供了一个Ti.Blob对象(event.media),我需要以某种方式将其发送到服务器.服务器总是响应一个JSON对象,因此该Blob必须以某种方式可以JSON序列化.
I'm stuck in a problem in a project for a client that requires images took on device (using the camera) to be sent to a WebService and afterwards be seen on any device using the app (both Android and iOS devices). Titanium provides a Ti.Blob object (event.media) after taking a picture (which is not JSON serializable) and I need somehow to send this to the server. The server responds always a JSON object, thus this Blob must be somehow JSON serializable.
我尝试了很多方法都没有成功:
I've tried many ways without success:
1-Base64编码Blob
1 - Base64Encode the Blob
var base64blob = Ti.Utils.base64encode(event.media);
不起作用,它卡住了应用程序并抛出ASL超出最大大小错误.我认为图像太大而无法进行base64编码.
Doesn't work, it stucks the app and throws a ASL exceeded maximum size error. I imagine that the image is too large to be base64encoded.
2-将Blob读入缓冲区
2 - Read the Blob into a Buffer
var blobStream = Ti.Stream.createStream({ source: event.media, mode: Ti.Stream.MODE_READ });
var buffer = Ti.createBuffer({ length: event.media.length });
var bytes = blobStream.read(buffer);
它可以工作,但是我不知道如何将保存图像内容的缓冲区转换为服务器可以在JSON对象中返回的内容,然后再次转换为Image Blob.
It works but I have no idea how can I transform this buffer holding the image contents into something that the server can return in a JSON object and later be transformed into an Image Blob again.
服务器无法管理Ti.Blob对象或Ti.Buffer对象,因为首先,它们是Titanium对象,并且服务器基于C#;其次,由于Ti.Blob和Ti.Buffer不是JSON,可序列化的,因此JSON返回无效.
The server can't manage Ti.Blob objects or Ti.Buffer objects because, first of all, they are Titanium objects and the server is C# based, and second due to Ti.Blob and Ti.Buffer aren't JSON serializable, thus the JSON return doesn't work.
我所需要的基本上在下面的虚构示例中进行描述:
What I need is basically described in the imaginary example below:
var imageBlob = event.media;
var JSONSerializableImg = imageBlob.toJSON();
sendImageToServer(JSONSerializableImg);
var imgFromServer = getImageFromServer();
var imageBlob = imgFromServer.toBlob();
var imgView = createImageView({
image: imageBlob
});
我希望有人可以在任何可能的转换方法方面为我提供帮助.
I hope someone can help me with any conversion method possible.
谢谢
推荐答案
我通过在服务器端专门创建一个单独的方法来上传照片来解决了此问题.我按照下面的链接访问服务器端:
I solved this issue by creating a separate method on server-side specially to upload photos. I followed the link below for server side:
在Titanium中,我必须这样设置XHR的标头:
In Titanium I had to set XHR's header like this:
this.xhr.setRequestHeader("ContentType", "image/png");
this.xhr.setRequestHeader('enctype', 'multipart/form-data');
this.xhr.setRequestHeader("ContentType", "image/png");
this.xhr.setRequestHeader('enctype', 'multipart/form-data');
就是这样! 感谢所有的答案.
That's it! Thank's for all the answers.
这篇关于无法使Image Blob JSON可序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!