2014-11-07 2 views
-3

У меня есть этот функционирующий код и я просто хочу, чтобы загрузить его в почтовый индекс вместо его первоначально файла Вот мой код:Grails скачать этот файл как почтовый

def downloadFile() { 
    def filePath=ServletContextHolder.servletContext.getRealPath("/") + "Audio/" 
    def sub = AudioClips.get(params.id) 
    filePath+=sub.fileName 
    def file = new File(filePath); 
    if (file.exists()) 
    { 
     response.setContentType("application/octet-stream") // or or image/JPEG or text/xml or whatever type the file is 
     response.setHeader("Content-disposition", "attachment;filename=\"${file.name}\"") 
     response.outputStream << file.bytes 
     redirect(controller: "category",action: "index") 
    } 
+0

Что вы пробовали? Что не работает над тем, что вы пробовали? SO не является местом, чтобы просить людей писать код для вас. Приложите немного усилий. Ниже приведен пример создания zip-файла для запуска: http://groovy-almanac.org/create-a-zipfile/ –

ответ

0
def downloadAudioZipFile = { 
    def filePath = ServletContextHolder.servletContext.getRealPath("/") + "Audio/" 
    def sub = AudioClips.get(params.id) 
    filePath += sub.fileName 
    def file = new File(filePath); 
    if (file.exists()) { 
     response.setContentType("application/octet-stream") 

     response.setHeader("Content-Disposition", "Attachment;Filename=\"${file.name}\".zip") 


     ZipOutputStream zip = new ZipOutputStream(response.outputStream); 
     def file2Entry = new ZipEntry("${file.name}"); 
     zip.putNextEntry(file2Entry); 
     zip.write(file.bytes) 
     zip.close(); 
    } 
}