tjweir (owner)

Revisions

gist: 128626 Download_button fork
public
Public Clone URL: git://gist.github.com/128626.git
Embed All Files: show embed
FileUpload with Lift. #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
First there is a snippet using the bind functionality.
var image : FileParamHolder = _
 
bind("widget", xhtml,
  "image" -> fileUpload(image = _)
 
}
 
Now write out:
 
val wbos : ByteArrayOutputStream = build(image)
val fout = new File(outputFilename)
if (fout.exists()) {
   Log.error("Tried to overwrite existing file")
   return "Error: Filename already exists"
}
 
val foStream = new FileOutputStream(fout)
foStream.write(wbos.toByteArray())
foStream.close()
 
def build(image : FileParamHolder) : ByteArrayOutputStream = {
  val bout = new ByteArrayOutputStream()
  val zipout : ZipOutputStream = new ZipOutputStream(bout)
  zipout.setMethod(ZipOutputStream.DEFLATED);
  zipout.setLevel(Deflater.DEFAULT_COMPRESSION);
 
  writeImage(zos, image)
 
}
 
private def writeImage(zos: ZipOutputStream,
image:FileParamHolder):Unit = {
    if (image.fileName == "") {
      Log.info("Provided image file is null")
      return
    }
    if (image.fileName.lastIndexOf("\\") != -1) //this is needed if
upload is from a windows system
      zos.putNextEntry(new ZipEntry("res/" + image.fileName.substring
(image.fileName.lastIndexOf("\\")+1)))
    else
      zos.putNextEntry(new ZipEntry("res/" + image.fileName))
    zos.write(image.file, 0, image.file.length) //make here a loop on
very large files
    zos.closeEntry()
 
}