2011-05-24 16 views
6

मैंने अपनी प्रोजेक्ट (पहली विधि) में किसी फ़ोल्डर में फ़ाइल कॉपी करने के लिए this विधि का उपयोग किया है, और मैंने इसे संपादित किया है ताकि स्थान कक्षा सबमिशन में मेरे 'स्थान' पर संग्रहीत किया जा सके (नीचे देखें)।Grails डाउनलोड फ़ाइल

अब मैं अपने दृश्य में एक छवि पर क्लिक करने के बाद, उस फ़ाइल को डाउनलोड करने में सक्षम होना चाहता हूं। मैं उसे कैसे कर सकता हूँ ?

class Submissions { 

    Date dateSub 
    String Location 
    String fileName 

} 

उत्तर

1

आपको बस प्रतिक्रिया पर बाइट प्रस्तुत करने की आवश्यकता है। हम

def streamFile = { 
    // load the attachment by id passed on params 
    .... 
    response.contentType = attachment.contentType.toLowerCase() 
    response.contentLength = attachment.data.length() 
    // our 'data' field is a Blob, the important thing here is to get the bytes according to 
    // how you get the actual downlaod 
    response.outputStream.write(attachment.data.getBytes(1L, attachment.data?.length() as int)) 
} 

हमारे नियंत्रक में कुछ करते हैं, और बस जीएसपी पर उस नियंत्रक विधि के लिए एक लिंक बनाते हैं। सामग्री प्रकार से आप कैसे बैठते हैं, इस पर निर्भर करता है कि ब्राउज़र आपके लिए सामान करेगा। उदाहरण के लिए यदि आपके पास छवि प्रकार है, तो यह छवि दिखाएगा। यदि आपके पास कोई शब्द दस्तावेज़ है, तो ब्राउज़र को उपयोगकर्ता के सिस्टम के लिए उपयुक्त प्रोग्राम खोलना चाहिए।

+1

एचएम आप यहां बाइट्स के बारे में बात कर रहे हैं .. फाइलें डेटाबेस में ही नहीं हैं, केवल इसकी जगह है। क्या आप इसे सही तरीके से ले रहे हैं? – Michael

+0

@ माइकल यदि आप डीबी में पथ संग्रहीत करते हैं, तो आपको फ़ाइल ऑब्जेक्ट प्राप्त करने की आवश्यकता होती है, और फिर बाइट प्राप्त करें। – hvgotcodes

20

मैं निम्न जैसा कुछ किया है:

मान लिया जाये कि अपने डाउनलोड पृष्ठ प्रासंगिक प्रविष्टियों उदाहरण है ...

<g:link action="downloadFile" id="${aSubmission.id}"> 
    <img>...etc...</img> 
</g:link> 

फिर नियंत्रक में (अपने "स्थान" फ़ाइल का पथ है?):

def downloadFile = { 
    def sub = Submissions.get(params.id) 
    def file = new File("${sub.location}/${sub.fileName") 
    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 
    } 
    else render "Error!" // appropriate error handling 
} 
+1

यह काम करता है, file.exists के लिए त्रुटि दें क्योंकि ऐसी कोई विधि नहीं है, यह file.exists() होना चाहिए – praveen2609

2

खैर, यह हमेशा बेहतर अपने डाउनलोड तर्क आज़माएं/कैच अंदर लपेटा जाता है और के रूप में भी झूठी webrequest सेट रखने के लिए है। http://lalitagarw.blogspot.in/2014/03/grails-forcing-file-download.html

def downloadFile() { 
    InputStream contentStream 
    try { 
     def file = new File("<path>") 
     response.setHeader "Content-disposition", "attachment; filename=filename-with-extension" 
     response.setHeader("Content-Length", "file-size") 
     response.setContentType("file-mime-type") 
     contentStream = file.newInputStream() 
     response.outputStream << contentStream 
     webRequest.renderView = false 
    } finally { 
     IOUtils.closeQuietly(contentStream) 
    } 
} 
संबंधित मुद्दे