如何通过 JSON API 通过 Carrierwave 上传文件? [英] How do I upload a file via Carrierwave over a JSON API?

查看:38
本文介绍了如何通过 JSON API 通过 Carrierwave 上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Rails 中构建文件操作 API,我需要能够通过单独的 gem 访问它.API 使用 Carrierwave,这部分工作没有问题.我不明白怎么做的是将任意文件从 gem 接口传递给 API.

I'm building a file-manipulation API in Rails, and I need to be able to access it via a separate gem. The API uses Carrierwave, and that piece works with no trouble. What I don't understand how to do is to take an arbitrary file and deliver it to the API from the gem interface.

Carrierwave 将其文件作为 File.open('foo.jpg') 的结果或作为来自文件字段的 POST.不过,我真的不确定两者在做什么来序列化文件并将其一起发送.

Carrierwave takes its files as either the results of File.open('foo.jpg') or as a POST from a file field. I'm really not sure what either is doing, though, to serialize the file and send it along.

如何获取文件的内容并将其转换为可以通过 JSON 传递和发布的内容?

How do I take the contents of a file and turn that into something I can pass around and post via JSON?

推荐答案

当 HTML 表单 POST 文件时,实际发生的是 HTTP 的一个特殊部分,称为多部分请求.实际上,文件会附加"到请求中.

When an HTML form POSTs a file, what actually happens is a special part of HTTP called a multipart request. Effectively, the file gets "attached" to the request.

该问题将由您用于将 JSON POST 到您的 api 的库来回答.将文件附加到请求应该相当普遍,但并非所有库都支持它.

The question will be answered by what library you are using to POST the JSON to your api. Attaching a file to a request should be fairly commonplace but not all libraries may support it.

这篇堆栈溢出文章好像给出一些关于如何做的很好的指示.

这篇关于如何通过 JSON API 通过 Carrierwave 上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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