문제

I am developing file upload web app in Scalatra. In my use case i want to check if the submitted request contains attachment or not.

Below is my code :

val (profilePicByteStream: Array[Byte], contentType:String, fileName) = fileParams.get("profilePic") match {
      case Some(file) => (file.get(), file.contentType.getOrElse("application/octet-stream"), file.name)
      case None => (Array(), "", null)
    }

It is working fine when there is an attachment in the submitted request. How to hanlde invalid request.

In case of non attachments, it is throwing error :

([Ljava.lang.Object;@16837c0b,) (of class scala.Tuple2)
scala.MatchError: ([Ljava.lang.Object;@16837c0b,) (of class scala.Tuple2)

In both cases with attachments and without attachments, it is going to Some(file) case.

Can anyone suggest how to handle invalid case?

도움이 되었습니까?

해결책

Have you considered doing a "map" instead of a match?

fileParams.get("profilePic").map(file => (file.get(), file.contentType.getOrElse("application/octet-stream"), file.name))

This will work in the cases when you do have a file uploaded, and will return None in the cases where nothing is present

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top