【问题标题】:Play Framework - Respond with JSON after uploading a file (multipartFormData)Play Framework - 上传文件后响应 JSON (multipartFormData)
【发布时间】:2021-09-13 07:58:29
【问题描述】:

我正在使用此代码将图像上传到服务器上,我从这个链接play upload获得的图像

 def upload = Action(parse.multipartFormData) { request =>
    request.body
      .file("picture")
      .map { picture =>
        val dataParts = request.body.dataParts;

        val filename = Paths.get(picture.filename).getFileName
        val fileSize = picture.fileSize
        val contentType = picture.contentType

         val picturePaths =
           picture.ref.copyTo(
             Paths.get(
               s"/opt/docker/images/$filename"
             ),
             replace = true
           )
        if (dataParts.get("firstPoint") == None) {
          val pointlocation = new Point_LocationModel(
            dataParts.get("step").get(0),
            dataParts.get("backgroundTargetName").get(0),
            dataParts.get("x").get(0),
            dataParts.get("y").get(0),
            dataParts.get("z").get(0),
            dataParts.get("rotation").get(0),
            dataParts.get("note").get(0),
            dataParts.get("tag").get(0),
            dataParts.get("work_session_id").get(0),
            (picturePaths).toString
          )
          point_LocationRepository.create(pointlocation).map { data =>
            Created(Json.toJson(data._2))
          }
        } else {
          val jValuefirstPoint =
            Json.parse(dataParts.get("firstPoint").get(0)).as[PointModel]
          val jValuesecondPoint =
            Json.parse(dataParts.get("secondPoint").get(0)).as[PointModel]

          val pointlocation = new Point_LocationModel(
            dataParts.get("step").get(0),
            dataParts.get("backgroundTargetName").get(0),
            Some(jValuefirstPoint),
            Some(jValuesecondPoint),
            dataParts.get("rotation").get(0),
            dataParts.get("note").get(0),
            dataParts.get("tag").get(0),
            dataParts.get("work_session_id").get(0),
            (picturePaths).toString
          )
          point_LocationRepository.create(pointlocation).map { data =>
            logger.info(s"repoResponse:  ${data}");
            Created(Json.toJson(data._2))
          }
        }
        Ok(s"picturePaths ${picturePaths}")

      }
      .getOrElse(Ok("Invalid Format"))
  }

此代码运行良好,但在响应方面我想从存储库中获取响应。我如何等待存储库的响应来返回这个?

你能告诉我我该怎么做吗? 提前致谢。

【问题讨论】:

  • 好吧,你尝试了什么?是什么阻碍了你?您已经拥有这两个信息(名称和大小),只需在响应中发送它即可。
  • 您好,我已经针对我已实现的特定代码更改了问题。提前致谢。

标签: scala playframework-2.0


【解决方案1】:

如果我们将您的代码简化为基本部分,您将拥有:

def upload = Action(parse.multipartFormData) { request =>
    request.body
      .file("picture")
      .map { picture =>
        if (someConditionA) {
          someBusinessLogicA().map { data =>
            Created(Json.toJson(data._2))
          }
        } else {
          someBusinessLogicB().map { data =>
            Created(Json.toJson(data._2))
          }
        }
        Ok(s"picturePaths ${picturePaths}")
      }
      .getOrElse(Ok("Invalid Format"))
}

有2个问题:

  • 您的if/else 的返回被Ok(s"picturePaths ${picturePaths}") 吞噬
  • 假设您的业务逻辑返回 Future[_],您需要在任何地方“传播”未来

这样的事情应该可以工作:

def upload = Action.async(parse.multipartFormData) { request =>
    request.body
      .file("picture")
      .map { picture =>
        if (someConditionA) {
          someBusinessLogicA().map { data =>
            Created(Json.toJson(data._2))
          }
        } else {
          someBusinessLogicB().map { data =>
            Created(Json.toJson(data._2))
          }
        }
      }
      .getOrElse(Future.successful(Ok("Invalid Format")))
}

注意getOrElse 中的Action.asyncFuture

【讨论】:

    猜你喜欢
    • 2014-07-11
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    相关资源
    最近更新 更多