无法使图像 Blob JSON 可序列化 [英] Unable to make an Image Blob JSON Serializable
问题描述
由于 base64 编码失败而创建的 JIRA 票证:https://jira.appcelerator.org/browse/TC-5876
JIRA Ticket created due to base64encode failure: https://jira.appcelerator.org/browse/TC-5876
我目前的 CFG:钛 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 在拍照后提供了一个 Ti.Blob 对象(event.media)(它不是 JSON 可序列化的),我需要以某种方式将它发送到服务器.服务器总是响应一个 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 - Base64Encode 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 对象中返回的内容,然后再次转换为图像 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');
就是这样!谢谢大家的回答.
That's it! Thank's for all the answers.
这篇关于无法使图像 Blob JSON 可序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!