设计具有二进制数据的其他网络服务最好的方法从浏览器中被消耗 [英] best approach to design a rest web service with binary data to be consumed from the browser

查看:169
本文介绍了设计具有二进制数据的其他网络服务最好的方法从浏览器中被消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发,将与来自Backbone.js的建立一个单一的网页应用程序消耗一个JSON休息Web服务

I'm developing a json rest web service that will be consumed from a single web page app built with backbone.js

这个API可以让相关的一些实体消费者上传文件,如与项目相关的PDF报告

This API will let the consumer upload files related to some entity, like pdf reports related to a project

周围的Googling,做在堆栈溢出一些研究,我来到这些可能的方法:

Googling around and doing some research at stack overflow I came with these possible approaches:

第一种方法: 的base64 EN codeD数据字段

POST: /api/projects/234/reports
{
  author: 'xxxx',
  abstract: 'xxxx',
  filename: 'xxxx',
  filesize: 222,
  content: '<base64 encoded binary data>'
}

第二条本办法:多部分窗体发布:

POST: /api/projects/234/reports
{
  author: 'xxxx',
  abstract: 'xxxx',
}

作为回应,我会得到一个报告编号,并与我出具另一篇文章

as a response I'll get a report id, and with that I shall issue another post

POST: /api/projects/234/reports/1/content
enctype=multipart/form-data

然后只是发送二进制数据

and then just send the binary data

(看看这个: http://stackoverflow.com/a/3938816/47633

第三种方法:将二进制数据发布到一个单独的资源,并保存在href

首先,我产生在客户端随机密钥,并发布二进制内容有

first I generate a random key at the client and post the binary content there

POST: /api/files/E4304205-29B7-48EE-A359-74250E19EFC4
enctype=multipart/form-data

然后

POST: /api/projects/234/reports
{
  author: 'xxxx',
  abstract: 'xxxx',
  filename: 'xxxx',
  filesize: 222,
  href: '/api/files/E4304205-29B7-48EE-A359-74250E19EFC4'
}

(看到这一点: http://stackoverflow.com/a/4032079/47633

我只是想知道是否有任何其他的方法,我可以使用,利弊/各的利弊,以及是否有处理这种要求的方式建立

I just wanted to know if there's any other approach I could use, the pros/cons of each, and if there's any established way to deal with this kind of requirements

大CON我看到第一种方法,是我有完全加载和连接的base64 code中的文件在客户端上

the big con I see to the first approach, is that I have to fully load and base64 encode the file on the client

一些有用的资源:

  • Post binary data to a RESTful application
  • What is a good way to transfer binary data to a HTTP REST API service?
  • How do I upload a file with metadata using a REST web service?
  • Bad idea to transfer large payload using web services?
  • http://stackoverflow.com/a/5528267/47633

推荐答案

我不认为把我的头顶部的任何其他方法。

I can't think of any other approaches off the top of my head.

3的办法,我和第3种方法的工作最多。我看到的最大的区别是第一种方法,另2之间:分离元数据和内容分为2资源

Of your 3 approaches, I've worked with method 3 the most. The biggest difference I see is between the first method and the other 2: Separating metadata and content into 2 resources


  • 专家:可扩展性

    • 当你的解决方案涉及张贴到同一台服务器,这样可以很容易地更改为指向的内容上传到一个单独的服务器(即亚马逊S3)

    • 在第一方法中,供应的元数据向用户同一服务器将具有阻止由大载的方法。


    • 上传失败(或元数据或内容)将离开孤立的数据在服务器DB

    • 孤立的数据可以清理与计划的作业,但是这会增加code复杂

    • 方法II降低了孤儿的可能性,在较长的客户等待时间成本,因为你阻塞后的第一个
    • 响应

    第一种方法似乎是最简单的,以code。不过,我只与第一种方法去,如果预计该服务被不经常使用,你可以设置用户的文件上传一个合理的限度。

    The first method seems the most straightforward to code. However, I'd only go with the first method if anticipate this service being used infrequently and you can set a reasonable limit on the user file uploads.

    这篇关于设计具有二进制数据的其他网络服务最好的方法从浏览器中被消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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