如何将大文件从表单上载到Phoenix? [英] How to upload a big file from a form to Phoenix?

查看:63
本文介绍了如何将大文件从表单上载到Phoenix?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有文件字段的HTML表单,该字段用于将文件上传到Phoenix应用程序中的/file路由.

I have an HTML form with a file field that is used to upload a file to a /file route in my Phoenix application.

我使用curl -v -F "file=@MyTestFile" http://localhost:4000/file/在命令行中模拟了此行为,以便进行更快的测试.

I mimic this behaviour from command line with curl -v -F "file=@MyTestFile" http://localhost:4000/file/ for faster testing.

当我使用大文件时(转折点似乎在7.7MB左右),我从Plug收到以下异常:

When I use a big file (turning point seems to be around 7.7MB), I get the following exception from Plug:

18:40:38.897 [错误]进程< 0.420.0>中的错误,退出值为:{[{reason,#{' exception '=> true,' struct '=>'Elixir.Plug.Parsers.RequestTooLargeError',message => nil}},{mfa,{'Elixir.Plug.Adapters.Cowboy.Handler',init,3}},{stacktrace,[ {'Elixir.Plug.Parsers',reduce,6,[{file,"lib/plug ...

18:40:38.897 [error] Error in process <0.420.0> with exit value: {[{reason,#{'exception'=>true,'struct'=>'Elixir.Plug.Parsers.RequestTooLargeError',message=>nil}},{mfa,{'Elixir.Plug.Adapters.Cowboy.Handler',init,3}},{stacktrace,[{'Elixir.Plug.Parsers',reduce,6,[{file,"lib/plug...

是否有一种允许较大文件上传的解决方法?

Is there a workaround to allow bigger files to be uploaded?

在Plug中似乎有一个:length选项关键字,但是如何从Phoenix设置它呢?选择8_000_000的特定值的原因是什么?

There seems to be a :length option keyword in Plug, but how could I set it from Phoenix? And what is the reason this particular value of 8_000_000 has been chosen?

推荐答案

您可以在config/config.exs文件中进行配置:

You can configure this in your config/config.exs file:

config :phoenix, MyApp.Router,
  ...
  parsers: [parsers: [:urlencoded, :multipart, :json],
            accept: ["*/*"],
            json_decoder: Poison,
            length: 100_000_000],

这篇关于如何将大文件从表单上载到Phoenix?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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