【问题标题】:How to fix 413 HTTP Request entity too large如何修复 413 HTTP 请求实体太大
【发布时间】:2016-07-09 13:28:13
【问题描述】:

我有一个项目,我有一个用 angularjs 编写的简单客户端,服务器在 scala 中,在我的客户端中,我有一个简单的上传按钮,您可以单击它并选择一个本地文件(csv),当我上传时某个 csv 有点大,比如我得到的 6000 行

413 请求实体太大

我上传的js代码是:

 $scope.upload = function(files) {
    if (!files || !files.length) {
    } else {
      for (var i = 0; i < files.length; i++) {
        var file = files[i];
        Upload.upload({
            url: '/uploadFile',
            file: file
        }).
        }).success(function (data, status, headers, config) {
        }).error(function (data, status, headers, config) {
            $scope.showServerError(status, data);
        })
      }
    }
  };

在服务器中是:

def uploadFile = Action(parse.multipartFormData) { implicit request =>

    request.body.file("file").fold {
      BadRequest("Missing file")
    } { uploadedFile => {

      val localFile = new File("/tmp/" + uploadedFile.ref.file.getName)

      Files.copy(uploadedFile.ref.file.toPath, localFile.toPath, StandardCopyOption.REPLACE_EXISTING)
      localFile.deleteOnExit()
      val j = Json.parse( ${Crypto.encryptAES(localFile.getAbsolutePath)}})
      Ok(j)
    }}
  }

为了支持更大的文件需要进行哪些更改?

【问题讨论】:

    标签: angularjs scala http playframework playframework-2.0


    【解决方案1】:

    您可以使用maxLength 正文解析器来指定正文的最大大小(以字节为单位):

    // Allow up to 15MB files...
    private val uploadParser = parse.maxLength(15*1024*1024, parse.multipartFormData)
    
    def uploadFile = Action(uploadParser) { implicit request =>
      ...
    }
    

    多部分表单数据的默认值为 10MB,您也可以通过更改 play.http.parser.maxDiskBuffer 设置来覆盖它。请参阅docs

    【讨论】:

    • 我试图设置 play.http.parser.maxMemoryBuffer=20M 因为我想支持最大 20 mb 的上传,但它没有工作......你知道为什么吗?我写的好吗?
    • @jackmiao 你试过换maxDiskBuffer吗?
    • 是的...这就是我在 application.conf 中 play.http.parser.maxMemoryBuffer=20M 所做的
    • 磁盘,不是内存:play.http.parser.maxDiskBuffer=20M ;-)
    猜你喜欢
    • 2012-09-23
    • 2013-01-16
    • 1970-01-01
    • 2014-12-30
    • 2014-12-23
    • 2021-03-05
    • 2019-06-14
    • 2018-08-20
    相关资源
    最近更新 更多