我有一个像这样的上传表单(form.gsp
):
<html>
<head>
<title>Upload Image</title>
<meta name="layout" content="main" />
</head>
<body>
<g:uploadForm action ="upload">
Photo: <input name="photos" type="file" />
<g:submitButton name="upload" value="Upload" />
</g:uploadForm>
</body>
</html>
我希望用户上传任何图片,当他单击上传
按钮时,我需要某种方法来在我的 Controller 操作中获取图像并将其传递给 View :
def upload = {
byte[] photo = params.photos
//other code goes here . . . .
}
这会引发错误:
Cannot cast object 'org.springframework.web.multipart.commons.CommonsMultipartFile@1b40272' with class 'org.springframework.web.multipart.commons.CommonsMultipartFile' to class 'byte'
请注意,我不希望这些照片保存在我的数据库中。实际上,一旦 upload
操作完成,我将处理该图像并在 upload
View 本身中显示输出。所以如果我有一个解决方案就好了。
提前致谢。
最佳答案
def reqFile = request.getFile("photos")
InputStream file = reqFile.inputStream
byte[] bytes = file.bytes
编辑:按照建议将 getBytes 更改为字节,这是更好的常规方式:)
关于grails - 如何从 Grails 中上传的文件中获取字节 (byte[]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8983922/