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

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

问题描述

我有一个REST的web服务,当前公开这个URL:

http:// server / data / media



其中用户可以 POST 以下JSON:

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

为了创建一个新的媒体元数据。 / p>

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



也有使用 multipart / form-data 就像一个HTML表单将发送,但我使用REST Web服务,我想坚持使用JSON如果可能的话。

解决方案

我同意Greg的观点,两阶段方法是一个合理的解决方案,但是我会这么做其他的方式。我会这样做:
$ b $ pre $ POST http:// server / data / media
body:
{
Name:Test,
Latitude:12.59817,
Longitude:52.12873
}

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

  201创建
位置:http:// server / data / media / 21323
{
Name:Test,
Latitude:12.59817,
经度:52.12873,
ContentUrl:http:// server / data / media / 21323 / content
}

然后客户端可以使用这个ContentUrl并用文件数据做一个PUT。



这个方法的好处是当你的服务器开始被大量的数据压缩后,你返回的url就可以指向其他有更多空间/容量的服务器。或者,如果带宽是一个问题,您可以实施某种循环方法。


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

http://server/data/media

where users can POST the following JSON:

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

in order to create a new Media metadata.

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.

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.

解决方案

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"
}

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

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天全站免登陆