gpt4 book ai didi

Scala Play 文件上传 : Cannot write an instance of views. html.uploadFile.type 到 HTTP 响应

转载 作者:行者123 更新时间:2023-12-04 14:10:53 24 4
gpt4 key购买 nike

我在 Play 中创建了一个允许用户上传文件的最小网站。虽然涉及的代码比较简单,但是我找不到这个编译错误的原因:

Cannot write an instance of views.html.uploadFile.type to HTTP response. Try to define a Writeable[views.html.uploadFile.type]

以下是 views.uploadFile.scala.html的内容:
@helper.form(action = routes.FileUpload.handleUpload, 'enctype -> "multipart/form-data") {
File to upload: <input type="file" name="uploadedFile">

<div> <input type="submit"> </div>
}

这是显示文件上传表单并处理上传文件的 Controller :
package controllers

import play.api.i18n.{MessagesApi, I18nSupport}
import play.api.mvc._
import javax.inject.Inject

class FileUpload @Inject()(val messagesApi: MessagesApi) extends Controller with I18nSupport {
def uploadForm = Action {
Ok(views.html.uploadFile)
}

def handleUpload = Action(parse.multipartFormData) { request =>
import java.io.File
request.body.file("uploadedFile") match {
case Some(file) =>
val fileName = file.filename
val uploadDir = "/tmp/uploadedFiles/"
file.ref.moveTo(new File(uploadDir + fileName))
Ok(s"File uploaded: $fileName")

case None =>
Redirect(routes.FileUpload.uploadForm()).flashing(
"error" -> "File invalid")
}
}
}

为了完整起见,这是我的 routes文件:
GET         /uploadForm          @controllers.FileUpload.uploadForm
POST /upload @controllers.FileUpload.handleUpload

最佳答案

我忽略了 Controller 操作中缺少的括号:

def uploadForm = Action {
// instead of Ok(view.html.uploadFile)
Ok(views.html.uploadFile())
}

添加它们使应用程序编译良好。

关于Scala Play 文件上传 : Cannot write an instance of views. html.uploadFile.type 到 HTTP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33720916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com