如何使用 REST Web 服务上传带有元数据的文件? [英] How do I upload a file with metadata using a REST web service?

查看:26
本文介绍了如何使用 REST Web 服务上传带有元数据的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个当前公开此 URL 的 REST Web 服务:

I have a REST web service that currently exposes this URL:

http://server/data/media

用户可以在其中POST以下JSON:

where users can POST the following JSON:

{
    "Name": "Test",
    "Latitude": 12.59817,
    "Longitude": 52.12873
}

为了创建新的媒体元数据.

in order to create a new Media metadata.

现在我需要能够在上传媒体元数据的同时上传文件.解决这个问题的最佳方法是什么?我可以引入一个名为 file 的新属性并对文件进行 base64 编码,但我想知道是否有更好的方法.

Now I need the ability to upload a file at the same time as the media metadata. What's the best way of going about this? I could introduce a new property called file and base64 encode the file, but I was wondering if there was a better way.

还有使用 multipart/form-data 就像 HTML 表单发送的内容一样,但我使用的是 REST Web 服务,如果可能的话,我想坚持使用 JSON.

There's also using multipart/form-data like what a HTML form would send over, but I'm using a REST web service and I want to stick to using JSON if at all possible.

推荐答案

我同意 Greg 的观点,即两阶段方法是一个合理的解决方案,但我会反过来做.我会这样做:

I agree with Greg that a two phase approach is a reasonable solution, however I would do it the other way around. I would do:

POST http://server/data/media
body:
{
    "Name": "Test",
    "Latitude": 12.59817,
    "Longitude": 52.12873
}

创建元数据条目并返回如下响应:

To create the metadata entry and return a response like:

201 Created
Location: http://server/data/media/21323
{
    "Name": "Test",
    "Latitude": 12.59817,
    "Longitude": 52.12873,
    "ContentUrl": "http://server/data/media/21323/content"
}

然后客户端可以使用此 ContentUrl 并使用文件数据执行 PUT.

The client can then use this ContentUrl and do a PUT with the file data.

这种方法的好处是,当您的服务器开始因大量数据而不堪重负时,您返回的 url 可以指向其他具有更多空间/容量的服务器.或者,如果带宽是一个问题,您可以实施某种循环方法.

The nice thing about this approach is when your server starts get weighed down with immense volumes of data, the url that you return can just point to some other server with more space/capacity. Or you could implement some kind of round robin approach if bandwidth is an issue.

这篇关于如何使用 REST Web 服务上传带有元数据的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆