goetas / multipart-upload-bundle
Goetas MultipartUploadBundle
Requires
- php: ^8.0
- riverline/multipart-parser: ^2.1
- symfony/dependency-injection: ^6.0|^7.0
- symfony/http-foundation: ^6.0|^7.0
- symfony/http-kernel: ^6.0|^7.0
Requires (Dev)
- matthiasnoback/symfony-dependency-injection-test: ^3.0|^4.0|^5.0
- phpunit/phpunit: ^9.5
README
Symfony multipart/related
、multipart/alternative
和 multipart/mixed
内容类型处理器。
此包实现了https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html 规范的一部分,并允许您使用 Symfony 处理 Content-Type: multipart/*;
请求。
安装
运行 composer require goetas/multipart-upload-bundle
将包添加到 symfony(如果未使用 symfony/flex)
请求格式
一个 multipart/related
请求可能如下所示
Host: localhost
Content-Type: multipart/related; boundary=19D523FB
--19D523FB
Content-Type: application/json
{
"content": "Some JSON content"
}
--19D523FB
Content-Type: image/png
Content-Disposition: form-data; name="image"; filename="image.jpg"
Content-MD5: 314ca078416a9b27efbe338ac5a2f727
... binary content...
Content-Type: text/html
Content-Disposition: form-data; name="content"
Content-MD5: 314ca078416a9b27efbe338ac5a2f727
<a href="https://github.com/goetas/MultipartUploadBundle">HTML content</a>
--19D523FB
Content-Type: image/png
Content-Disposition: attachment; filename="image.jpg"
Content-MD5: 314ca078416a9b27efbe338ac5a2f727
... binary content...
--19D523FB
Content-Type: octet/stream
X-Custom-Header: header value
... binary content...
--19D523FB--
使用方法
控制器
Body 不会自动解码,您可以自行解码或使用 FOSRestBundle 来透明地处理
public function indexAction(Request $request) { if ('application/json' == $request->headers->get('content-type')) { $data = json_decode($request->getContent(), true); } }
表单字段
具有 form-data; name=
的部分在 Content-Disposition
部分的头部将被视为一个常规上传文件。
$html = $request->request->get('content');
可用于与 Symfony 的表单构建器一起使用
$builder->add('content', TextAreaType::class);
上传文件
具有 form-data; name=
和 filename=
的部分在 Content-Disposition
部分的头部将被视为一个常规上传文件。
$file = $request->files->get('image');
可用于与 Symfony 的表单构建器一起使用
$builder->add('image', FileType::class);
附件文件
具有 attachment; filename=
的部分在 Content-Disposition
部分的头部将被视为一个附件文件。
$attachment = $request->attributes->get('attachments')[0];
相关部分
没有 filename
的部分将被视为 Riverline\MultiPartParser\StreamedPart
对象。也可以通过 related-parts
属性访问所有部分。
$part = $request->attributes->get('related-parts')[0];
- 获取部分的头部
$headers = $part->getHeaders()->all();
- 获取部分的内容
$content = $part->getContent();
- 获取部分的内容作为资源
$content = stream_get_contents($part->getContent(true));
- 第一个部分注入
默认情况下,当消息是 multipart/*
时,第一个部分将成为 Symfony 消息内容。您可以通过将 first_part_as_default
设置为 false
来禁用此功能。
$content = $request->getContent(); // content of the first part, not the whole message
配置
goetas_multipart_upload: first_part_as_default: true
注意
本项目的代码在 MIT 许可下提供。如需专业支持,请联系 goetas@gmail.com 或访问 https://www.goetas.com