如何在播放框架中处理fileupload到表中? [英] how to handle fileupload into a table in play framework?

查看:171
本文介绍了如何在播放框架中处理fileupload到表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为系统的表,它有一些列主要是数字/字符类型,但有一个二进制类型用于存放一个文档(word或excel等)

忽略现在如果我们应该保持文件内联或外部ie bfile或blob ...问题是更多关于如何处理它在播放框架



我已经得到窗体使用模型视图和控制器...但我设置它没有文件字段只是为了得到它的工作:

我在模型中有一个case类

  case class System(sys_id:Pk [Long] = NotAssigned 
,sys_name:String
,sys_desc :String
,sys_owner1:Long
,sys_owner2:Long)



$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' NotAssigned:Pk [Long]),
sys_name - > nonEmptyText,
sys_desc - > nonEmptyText,
sys_owner1 - > longNumber,
sys_owner2 - > (System.apply)(System.unapply)



并在视图中的一个表单

$ $ p $ code @form(routes.Systems.save(),'class - >form-horizo​​ntal ){
@inputText(systemForm(sys_name),'_label - >System Name)
@inputText(systemForm(sys_desc),_label - >Description)
@select(
systemForm(sys_owner1),
persons,
'_label - >主要所有者,_default - > - 选择一个人 - - ,
'_showConstraints - > false

@select(
systemForm(sys_owner2),
persons,
'_label - > ;Secondary Owner,_default - > - 选择一个人 - ,
'_showConstraints - > false

< div class =control-group >
< div class =controls>
< input type =submitvalue =Create Systemclass =btn btn-success>或
< a href =@ routes.Systems.list()class =btn>取消< / a>
< / div>
< / div>
}

这一切都很好...但现在我想添加文件, 我该怎么办?
我看过这个 http://www.playframework.com/documentation/2.1 .2 / ScalaFileUpload
,但它使我感到困惑,它没有提及如何或如何改变案例类 - 什么变量是存储在文件(如果是),以便我可以访问它在我的代码?

我应该修改我的案例类(如果是的话,我应该添加什么类型)?
我应该修改控制器中的表单(再次如果是的话,我应该添加什么类型的?)

我不打扰我是否内联存储或作为bfile - 但如何处理,如上所述?



谢谢

解决方案

有些项目需要考虑:


  1. .scala.html 文件,您需要一个文件上传字段 < input type =filename =someName> 。您需要按照文档中的说明设置表单的编码类型

  2. enctype - > multipart / form-data。 然后,您可以将您的控制器更改为处理multipart form-data ,所以它看起来像例子中的那个。使用 Action(parse.multipartFormData)来解析表单。我想你应该也可以使用 systemForm.bindFromRequest
    ),尽管我之前没有尝试过。


编辑:
$ b 我的意思是对待上传的文件和其他



尝试更改示例窗体文档,如下所示:

  def upload = Action(parse.multipartFormData){request => 
request.body.file(picture)。map {picture =>
import java.io.File
val filename = picture.filename $ b $ val contentType = picture.contentType
picture.ref.moveTo(new File(/ tmp / picture) )

//这是新的部分
val formContents = systemForm.bindFromRequest.value
//现在你可以用`formContents`和`picture`来做东西

Ok(File uploaded)
} .getOrElse {
Redirect(routes.Application.index).flashing(
error - >Missing file

}

}

i have a table called system it has some columns mainly number / char types but there is one binary type for holding a document (word or excel etc)

ignore for now if we should hold the file inline or external i.e. bfile or blob... the question is more about how to handle it in the play framework

I have got the form working with the model view and controller... but i set it up without the file-field just to get it working:

I have a case class in the model

case class System(sys_id: Pk[Long] = NotAssigned 
  , sys_name: String 
  , sys_desc: String 
  , sys_owner1: Long 
  , sys_owner2: Long) 

a form in the controller

val systemForm = Form(
    mapping(
      "sys_id" -> ignored(NotAssigned:Pk[Long]),
      "sys_name" -> nonEmptyText,
      "sys_desc" -> nonEmptyText,
      "sys_owner1" -> longNumber,
      "sys_owner2" -> longNumber
    )(System.apply)(System.unapply)
  )

and a form in the view

@form(routes.Systems.save(), 'class -> "form-horizontal") {  
            @inputText(systemForm("sys_name"), '_label -> "System Name")
            @inputText(systemForm("sys_desc"), '_label -> "Description")
            @select(
                systemForm("sys_owner1"), 
                persons, 
                '_label -> "Primary Owner", '_default -> "-- Choose a person --",
                '_showConstraints -> false
            )
            @select(
                systemForm("sys_owner2"), 
                persons, 
                '_label -> "Secondary Owner", '_default -> "-- Choose a person --",
                '_showConstraints -> false
            )
            <div class="control-group">
              <div class="controls">
                 <input type="submit" value="Create System" class="btn btn-success"> or 
                 <a href="@routes.Systems.list()" class="btn">Cancel</a> 
              </div>
            </div>
    }

This all works well... but now i want to add the file in, what should i do? I have seen this http://www.playframework.com/documentation/2.1.2/ScalaFileUpload but it confuses me it makes no mention of if or how i should change the case class - and what variable is the file stored in (if it is) so that I can access it in my code?

should I modify my case class (if so what type should i add)? should I modify the Form in the controller (again if so what type should I add?)

im not bothered whether I store it inline or as a bfile - but how can I handle it as asked above?

Thank you

解决方案

There are some items to consider:

  1. In your .scala.html file, you'll need a file upload field <input type="file" name="someName">.

  2. You'll need to set the encoding type of the form as stated in the documentation: 'enctype -> "multipart/form-data".

  3. You can then change your controller to to handle multipart form-data, so it looks like the one in the example. Use Action(parse.multipartFormData) to parse the form. I think you should then also be able to use your form with systemForm.bindFromRequest (from Form API), although I haven't tried that before.

Edit:

What I meant was to treat the uploaded file and the rest of the form data as separate items.

Try changing the example form the documentation to look like this:

def upload = Action(parse.multipartFormData) { request =>
  request.body.file("picture").map { picture =>
    import java.io.File
    val filename = picture.filename 
    val contentType = picture.contentType
    picture.ref.moveTo(new File("/tmp/picture"))

    // this is the new part
    val formContents = systemForm.bindFromRequest.value
    // now you can do stuff with `formContents` and `picture`

    Ok("File uploaded")
  }.getOrElse {
    Redirect(routes.Application.index).flashing(
      "error" -> "Missing file"
    )
  }

}

这篇关于如何在播放框架中处理fileupload到表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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