I'm trying to upload a file and send it as an attachment with an email. My code is working on windows OS but when I try to run it on Linux server, it creates the file but cannot attach it to the email. Any idea why this happens under Linux server? Below is my code:
class ClientUpload extends Logger {
object excelFile extends RequestVar[Box[FileParamHolder]](Empty)
private val aRandomTargetFile = Random.nextLong().toString + ".xls"
private def importExcel(name:String,sheetName:String): List[String] = {
println("import excel sheet="+sheetName+"name="+name)
var response : List[String] = Nil
var currRow = 0
if(sheetName == "Form")
currRow = firstRow
else if(sheetName == "Form2")
currRow = firstRow+1
if(currRow != 0){
try{
var workbook = new HSSFWorkbook(new FileInputStream(name))
val sheet = workbook.getSheet(sheetName)
try{
var endOfSheet_? : Boolean = false
val style = workbook.createCellStyle();
style.setFillBackgroundColor(HSSFColor.RED.index)
style.setFillPattern(1)
while (!endOfSheet_?) {
val row = sheet.getRow(currRow)
println("row="+row.getCell(0))
if (row != null &&
row.getCell(0) != null &&
row.getCell(0).getNumericCellValue() > 0 ) {
info("Processing row "+ currRow)
}
else
{
endOfSheet_? = true
}
currRow = currRow + 1
}
}catch {
case e: Exception => { }
case _ => { response = "Problem at cell 0 of row " + currRow :: response }
}
var fos: FileOutputStream = null;
fos = new FileOutputStream(new File("/tmp/"+aRandomTargetFile));
workbook.write(fos);
} catch {
case e: IOException => { response = "Problem opening or closing the excel sheet : " + e.getMessage() :: response }
case _ => { response = "Problem opening or closing the excel sheet : " + sheetName :: response}
}
}
response.reverse
}
def render ={
def process() {
excelFile.is match {
case Empty => S.error(S.?("upload.error.missing"))
case Full(exf) => {
var result : List[String] = Nil
info("Proccessing excel file")
info("Meme type="+exf.mimeType)
val basepath = "/tmp/"
val aRandomTargetFile = Random.nextInt(Integer.MAX_VALUE) + ".xls"
val oFile = new File(basepath, aRandomTargetFile)
val output = new FileOutputStream(oFile)
output.write(exf.file)
output.close()
result = result ++ importExcel(oFile.getPath(),"Form")
if(!result.isEmpty)
S.error(<li>{"Found the following problems:"}</li>++result.map(r => <li>{r}</li>))
else
sendEmail() &
S.notice("Successfully sent your upload.")
}
case _ => S.error(S.?("upload.error"))
}
}
uploadExcel &
"type=submit" #> SHtml.onSubmitUnit(process)
}
def uploadExcel: CssBindFunc = {
(S.get_?, excelFile.is) match {
case (true, _) => "name=file" #> SHtml.fileUpload(s => excelFile(Full(s)),("class" -> "btn btn-file"))
case (_, Empty) => "name=file" #> SHtml.fileUpload(s => excelFile(Full(s)),("class" -> "btn btn-file"))
case (false, _) => "name=file" #> SHtml.link(S.hostAndPath + "/tmp/" + aRandomTargetFile,() => Unit , <span></span>) &
"type=submit" #> SHtml.ajaxButton("Upload another file", ()=>S.redirectTo("clientupload"))
}
}
def sendEmail(): JsCmd = {
val body = sroiEmailBody
val fileLoc="/tmp/"+aRandomTargetFile
println(fileLoc)
val msg =
for ( bytes <- LiftRules.loadResource(fileLoc) )
yield XHTMLPlusImages(<p>Dear user,<br/><br/>The enclosed file has been sent for review</p>,PlusImageHolder(aRandomTargetFile, "text/csv", bytes) )
msg match {
case Full(m) =>
Mailer.sendMail(
From(emailFrom),
Subject("Upload"),
To(emailTo),
CC(userEmail),
PlainMailBodyType(body),
m)
case _ =>Alert("There was a problem !")
}
JsCmds.RedirectTo("clientupload")
}