简体   繁体   English

在Rest Web服务请求中返回文件的正确方法是什么?

[英]What is the correct way of returning a file in a Rest webservice request?

I need to return a file in my webservice. 我需要在Web服务中返回文件。 I am bit in doubt of what is the best/correct/execpted way. 我对什么是最佳/正确/执行方式感到怀疑。 (I am using JSON). (我正在使用JSON)。

I can: 我可以:

  1. simply return the file (as normal download) with the correct mime type, etc. via HTTP 只需通过HTTP返回具有正确的mime类型的文件(作为正常下载),等等。
  2. encode de file (eg: Base64 or other) and send it inside the JSON answer 编码文件(例如:Base64或其他文件)并在JSON答案中发送
  3. return a json answer with the url where the file can be found. 返回带有可在其中找到文件的url的json答案。

All three are valid, depending on the client's needs, so why not make it the client's choice? 这三个都是有效的,具体取决于客户的需求,那么为什么不让它成为客户的选择呢?

Let's say it's an image/jpeg file: 假设这是一个image/jpeg文件:

If the client sent the Accept: image/jpeg header, you return the file as a normal download. 如果客户端发送了Accept: image/jpeg标头,则将文件作为正常下载返回。

If the client sent the Accept: image/jpeg header and the Accept-Encoding: base64 , you return it as a normal download, Base64 encoded. 如果客户端发送了Accept: image/jpeg标头和Accept-Encoding: base64 ,则将其作为常规下载(Base64编码)返回。

If the client sends the Accept: text/uri-list header, you return a plain text response with the URI for the image stored somewhere else. 如果客户端发送Accept: text/uri-list标头,则返回纯文本响应,其中包含存储在其他位置的图像的URI。

There are no standard mimetypes to return base64 or links inside json, but you can use some standard of your own and create a vnd mimetype, as long as you document it. 没有标准的mimetypes返回json中的base64或链接,但是您可以使用自己的一些标准并创建vnd mimetype,只要您对其进行了说明即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM