Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

json - 最好以JSON形式将文件和关联数据发布到RESTful WebService(Posting a File and Associated Data to a RESTful WebService preferably as JSON)

This is probably going to be a stupid question but I'm having one of those nights.

(这可能是一个愚蠢的问题,但是我有一个夜晚。)

In an application I am developing RESTful API and we want the client to send data as JSON.

(在一个应用程序中,我正在开发RESTful API,我们希望客户端将数据作为JSON发送。)

Part of this application requires the client to upload a file (usually an image) as well as information about the image.

(此应用程序的一部分要求客户端上载文件(通常是图像)以及有关该图像的信息。)

I'm having a hard time tracking down how this happens in a single request.

(我很难追踪单个请求中的情况。)

Is it possible to Base64 the file data into a JSON string?

(是否可以将文件数据Base64转换为JSON字符串?)

Am I going to need to perform 2 posts to the server?

(我需要在服务器上执行2个帖子吗?)

Should I not be using JSON for this?

(我不应该为此使用JSON吗?)

As a side note, we're using Grails on the backend and these services are accessed by native mobile clients (iPhone, Android, etc), if any of that makes a difference.

(附带说明一下,我们在后端使用Grails,并且本机移动客户端(iPhone,Android等)可以访问这些服务(如果有区别的话)。)

  ask by Gregg translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I asked a similar question here:

(我在这里问了类似的问题:)

How do I upload a file with metadata using a REST web service?

(如何使用REST Web服务上载带有元数据的文件?)

You basically have three choices:

(您基本上有三个选择:)

  1. Base64 encode the file, at the expense of increasing the data size by around 33%, and add processing overhead in both the server and the client for encoding/decoding.

    (Base64对文件进行编码,其代价是将数据大小增加了约33%,并在服务器和客户端中增加了用于编码/解码的处理开销。)

  2. Send the file first in a multipart/form-data POST, and return an ID to the client.

    (首先在multipart/form-data POST中发送文件,然后将ID返回给客户端。)

    The client then sends the metadata with the ID, and the server re-associates the file and the metadata.

    (然后,客户端发送带有ID的元数据,然后服务器将文件和元数据重新关联。)

  3. Send the metadata first, and return an ID to the client.

    (首先发送元数据,然后将ID返回给客户端。)

    The client then sends the file with the ID, and the server re-associates the file and the metadata.

    (然后,客户端发送带有ID的文件,然后服务器将文件和元数据重新关联。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...