Need help in displaying the text file which is already stored in the database!!

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Need help in displaying the text file which is already stored in the database!!

saravanataee
Hi all,
                  I have a question. In one of my project i developed a form where i get user details along with a text file upload option. In the same form i have a show option where the user will be able to view all the data's he just given. The problem is i m not able to display the file link. I want to display the file link from database and allow them to download by clicking on the link..

Any help is appreciated!!

Thanks!

--
Cheers,

Saravana

Reply | Threaded
Open this post in threaded view
|

Re: Need help in displaying the text file which is already stored in the database!!

Nathan Wells
Others may be expert enough to tell you exactly where your problem lies, but I'd need more details. For instance, can you be more specific about "i m not able to display the file link"? Have you verified the existence of the file on the file system? What errors do you get? Does the link render at all? Can we see the code where you're generating the link, along with appropriate descriptions of where the data is coming from to make the link?

Nathan Wells


On Sat, Aug 18, 2012 at 11:06 AM, Saravanan S <[hidden email]> wrote:
Hi all,
                  I have a question. In one of my project i developed a form where i get user details along with a text file upload option. In the same form i have a show option where the user will be able to view all the data's he just given. The problem is i m not able to display the file link. I want to display the file link from database and allow them to download by clicking on the link..

Any help is appreciated!!

Thanks!

--
Cheers,

Saravana


Reply | Threaded
Open this post in threaded view
|

Re: Need help in displaying the text file which is already stored in the database!!

saravanataee
Thanks!

Let me describe more precisely. I meant I have a Gsp page where i have 10 fields and save and clear button. When the user enters the details in the given field and clicks submit. The data's will be stored in the database. I am also displaying all the data's given by the user in the same page. My question is, i m able to display all the fields easily but the fileupload i cannot.   I mean like other field values i want to display the link of the file from database.

any help!!


On Sun, Aug 19, 2012 at 8:42 AM, Nathan Wells <[hidden email]> wrote:
Others may be expert enough to tell you exactly where your problem lies, but I'd need more details. For instance, can you be more specific about "i m not able to display the file link"? Have you verified the existence of the file on the file system? What errors do you get? Does the link render at all? Can we see the code where you're generating the link, along with appropriate descriptions of where the data is coming from to make the link?

Nathan Wells



On Sat, Aug 18, 2012 at 11:06 AM, Saravanan S <[hidden email]> wrote:
Hi all,
                  I have a question. In one of my project i developed a form where i get user details along with a text file upload option. In the same form i have a show option where the user will be able to view all the data's he just given. The problem is i m not able to display the file link. I want to display the file link from database and allow them to download by clicking on the link..

Any help is appreciated!!

Thanks!

--
Cheers,

Saravana





--
Cheers,

Saravana

Reply | Threaded
Open this post in threaded view
|

Re: Need help in displaying the text file which is already stored in the database!!

micke_
There is loads about this on the internet, google "grails file download" and the first hit from stackoverflow shows you pretty much what you need to do.
It is always good if you google your problem and then try it out the suggested solutions. Then you can always include the code you've tried and get feed back on that from the mailing list.

Here is something to get you started.
You need to create an action which takes the primary key of the database record you want to return the file data for. Then you simply read your object from the DB and write the content you want to return to the output stream.


class FileController{

    def download() {
        def myObj = MyObj.find(params.id)
       
        //Whatever your content type is
        response.setContentType("text/plain")
        response.setHeader("Content-disposition", "filename=${myObj.name}")
        response.outputStream << myObj.data
       
        return
    }
}

<g:link controller="file" action="download" id="1"/>

In some old code of mine I also have (but this isn't needed) :

// add the next line to your file's imports part
 // import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
// the following obtains the instance of the GrailsWebRequest and disables standard view processing logic
def webRequest = request.getAttribute(GrailsApplicationAttributes.WEB_REQUEST)
 webRequest.setRenderView(false)

Can't quite remeber why I put this in and/or if it is needed (this code isn't used anymore). Wit the above I also flushed and closed the output stream.

Good luck,
 micke

On 19 August 2012 08:16, Saravanan S <[hidden email]> wrote:
Thanks!

Let me describe more precisely. I meant I have a Gsp page where i have 10 fields and save and clear button. When the user enters the details in the given field and clicks submit. The data's will be stored in the database. I am also displaying all the data's given by the user in the same page. My question is, i m able to display all the fields easily but the fileupload i cannot.   I mean like other field values i want to display the link of the file from database.

any help!!



On Sun, Aug 19, 2012 at 8:42 AM, Nathan Wells <[hidden email]> wrote:
Others may be expert enough to tell you exactly where your problem lies, but I'd need more details. For instance, can you be more specific about "i m not able to display the file link"? Have you verified the existence of the file on the file system? What errors do you get? Does the link render at all? Can we see the code where you're generating the link, along with appropriate descriptions of where the data is coming from to make the link?

Nathan Wells



On Sat, Aug 18, 2012 at 11:06 AM, Saravanan S <[hidden email]> wrote:
Hi all,
                  I have a question. In one of my project i developed a form where i get user details along with a text file upload option. In the same form i have a show option where the user will be able to view all the data's he just given. The problem is i m not able to display the file link. I want to display the file link from database and allow them to download by clicking on the link..

Any help is appreciated!!

Thanks!

--
Cheers,

Saravana





--
Cheers,

Saravana


Reply | Threaded
Open this post in threaded view
|

Re: Need help in displaying the text file which is already stored in the database!!

saravanataee
Hi,
               If possible can you explain me this part of code. I dont understand where i am actually reading the data from database and how to specify the name of the file!!

def download() {
        def myObj = MyObj.find(params.id)
       
        //Whatever your content type is
        response.setContentType("text/
plain")
        response.setHeader("Content-disposition", "filename=${myObj.name}")
        response.outputStream << myObj.data
       
        return
    }


On Sun, Aug 19, 2012 at 1:27 PM, Mikael Andersson <[hidden email]> wrote:
There is loads about this on the internet, google "grails file download" and the first hit from stackoverflow shows you pretty much what you need to do.
It is always good if you google your problem and then try it out the suggested solutions. Then you can always include the code you've tried and get feed back on that from the mailing list.

Here is something to get you started.
You need to create an action which takes the primary key of the database record you want to return the file data for. Then you simply read your object from the DB and write the content you want to return to the output stream.


class FileController{

    def download() {
        def myObj = MyObj.find(params.id)
       
        //Whatever your content type is
        response.setContentType("text/plain")
        response.setHeader("Content-disposition", "filename=${myObj.name}")
        response.outputStream << myObj.data
       
        return
    }
}

<g:link controller="file" action="download" id="1"/>

In some old code of mine I also have (but this isn't needed) :

// add the next line to your file's imports part
 // import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
// the following obtains the instance of the GrailsWebRequest and disables standard view processing logic
def webRequest = request.getAttribute(GrailsApplicationAttributes.WEB_REQUEST)
 webRequest.setRenderView(false)

Can't quite remeber why I put this in and/or if it is needed (this code isn't used anymore). Wit the above I also flushed and closed the output stream.

Good luck,
 micke


On 19 August 2012 08:16, Saravanan S <[hidden email]> wrote:
Thanks!

Let me describe more precisely. I meant I have a Gsp page where i have 10 fields and save and clear button. When the user enters the details in the given field and clicks submit. The data's will be stored in the database. I am also displaying all the data's given by the user in the same page. My question is, i m able to display all the fields easily but the fileupload i cannot.   I mean like other field values i want to display the link of the file from database.

any help!!



On Sun, Aug 19, 2012 at 8:42 AM, Nathan Wells <[hidden email]> wrote:
Others may be expert enough to tell you exactly where your problem lies, but I'd need more details. For instance, can you be more specific about "i m not able to display the file link"? Have you verified the existence of the file on the file system? What errors do you get? Does the link render at all? Can we see the code where you're generating the link, along with appropriate descriptions of where the data is coming from to make the link?

Nathan Wells



On Sat, Aug 18, 2012 at 11:06 AM, Saravanan S <[hidden email]> wrote:
Hi all,
                  I have a question. In one of my project i developed a form where i get user details along with a text file upload option. In the same form i have a show option where the user will be able to view all the data's he just given. The problem is i m not able to display the file link. I want to display the file link from database and allow them to download by clicking on the link..

Any help is appreciated!!

Thanks!

--
Cheers,

Saravana





--
Cheers,

Saravana





--
Cheers,

Saravana

Reply | Threaded
Open this post in threaded view
|

Re: Need help in displaying the text file which is already stored in the database!!

micke_
It is very difficult to answer questions about your code when we don't know how it works.

Is the project you are working on a Grails project where the data you are wanting to display is in a Grails domain object?

The code below is just an example using an example domain object where the primary key is "id" , a "name" field/column with what will be the default filename and where the file content field/column is "data". Using this information you should be able to apply this code example to your own domain model.

If you have difficulties please post code here showing what you are trying to do along with the error message you are getting.

- micke

On 19 August 2012 09:31, Saravanan S <[hidden email]> wrote:
Hi,
               If possible can you explain me this part of code. I dont understand where i am actually reading the data from database and how to specify the name of the file!!


def download() {
        def myObj = MyObj.find(params.id)
       
        //Whatever your content type is
        response.setContentType("text/
plain")
        response.setHeader("Content-disposition", "filename=${myObj.name}")
        response.outputStream << myObj.data
       
        return
    }


On Sun, Aug 19, 2012 at 1:27 PM, Mikael Andersson <[hidden email]> wrote:
There is loads about this on the internet, google "grails file download" and the first hit from stackoverflow shows you pretty much what you need to do.
It is always good if you google your problem and then try it out the suggested solutions. Then you can always include the code you've tried and get feed back on that from the mailing list.

Here is something to get you started.
You need to create an action which takes the primary key of the database record you want to return the file data for. Then you simply read your object from the DB and write the content you want to return to the output stream.


class FileController{

    def download() {
        def myObj = MyObj.find(params.id)
       
        //Whatever your content type is
        response.setContentType("text/plain")
        response.setHeader("Content-disposition", "filename=${myObj.name}")
        response.outputStream << myObj.data
       
        return
    }
}

<g:link controller="file" action="download" id="1"/>

In some old code of mine I also have (but this isn't needed) :

// add the next line to your file's imports part
 // import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
// the following obtains the instance of the GrailsWebRequest and disables standard view processing logic
def webRequest = request.getAttribute(GrailsApplicationAttributes.WEB_REQUEST)
 webRequest.setRenderView(false)

Can't quite remeber why I put this in and/or if it is needed (this code isn't used anymore). Wit the above I also flushed and closed the output stream.

Good luck,
 micke


On 19 August 2012 08:16, Saravanan S <[hidden email]> wrote:
Thanks!

Let me describe more precisely. I meant I have a Gsp page where i have 10 fields and save and clear button. When the user enters the details in the given field and clicks submit. The data's will be stored in the database. I am also displaying all the data's given by the user in the same page. My question is, i m able to display all the fields easily but the fileupload i cannot.   I mean like other field values i want to display the link of the file from database.

any help!!



On Sun, Aug 19, 2012 at 8:42 AM, Nathan Wells <[hidden email]> wrote:
Others may be expert enough to tell you exactly where your problem lies, but I'd need more details. For instance, can you be more specific about "i m not able to display the file link"? Have you verified the existence of the file on the file system? What errors do you get? Does the link render at all? Can we see the code where you're generating the link, along with appropriate descriptions of where the data is coming from to make the link?

Nathan Wells



On Sat, Aug 18, 2012 at 11:06 AM, Saravanan S <[hidden email]> wrote:
Hi all,
                  I have a question. In one of my project i developed a form where i get user details along with a text file upload option. In the same form i have a show option where the user will be able to view all the data's he just given. The problem is i m not able to display the file link. I want to display the file link from database and allow them to download by clicking on the link..

Any help is appreciated!!

Thanks!

--
Cheers,

Saravana





--
Cheers,

Saravana





--
Cheers,

Saravana


Reply | Threaded
Open this post in threaded view
|

Re: Need help in displaying the text file which is already stored in the database!!

saravanataee
Hi thanks,
                      Yes, your right. I am working on a grails project with Intellij Idea. And my domain class is
class ULearningUpload {

    static mapping = {
        table 'tuser_learning_upload'
        // version is set to false, because this isn't available by default for legacy databases
        version false
        id generator:'identity', column:'learning_upload_id'
        userID column: 'user_id'

        uLearning column: 'learning_id'
        learningUpload column: 'learningupload'

    }
    static belongsTo = ULearning


    ULearning uLearning

    Integer userID;

      byte[] learningUpload;

    Integer isActive
    Integer createdBy
    Date createdTimestamp
    Integer modifiedBy
    Date modifiedTimestamp
    static constraints = {
        userID(size: 0..25)
        isActive(nullable: true, max: 2147483647)
        createdBy(nullable: true, max: 2147483647)
        createdTimestamp(nullable: true)
        modifiedBy(nullable: true, max: 2147483647)
        modifiedTimestamp(nullable: true)
    }
    String toString() {
        return "${learningUpload }"
    }
}


Here the learningupload is what the field i used for uploading. In my Gsp i am creating a table for all fields like this..

 <td valign="top">${i+1}</td>

                                                        <td valign="top">${fieldValue(bean: learningDomainInstance.learningTypeCodeDomain, field: "learningType")}</td>

                                                        <td valign="top">${fieldValue(bean: learningDomainInstance, field: "learningTitle")}</td>


                                                        <td valign="top">${fieldValue(bean: learningDomainInstance, field: "learningDescription")}</td>

                                                        <td valign="top">${fieldValue(bean: learningDomainInstance, field: "learningInitiaterWords")}</td>

                                                        <td valign="top">${fieldValue(bean: learningDomainInstance, field: "learningSourceReference")}</td>


                                                    <td valign="top"><g:link action="DownloadFile" id="${learningDomainInstance.id}">                 </g:link></td>


                                                        <td valign="top"> <g:link controller="initiateLearning" action="DownloadFile">Download</g:link>  </td>

                                                        <td valign="top"><g:link action="initiateLearning" id="${learningDomainInstance.id}"><img src="${resource(dir:'/images', file:'edit.png')}"
                                                                                                                                                  style="vertical-align:middle;cursor:pointer "/></g:link></td>


Similarly. As helped by you, i created a download action method which looks like this. Declared inside the controller..


def DownloadFile = {
    def file = new File("C:\\Users\\Saravana\\Desktop\\mailbody.txt")

    if (file.exists()) {
        response.setContentType("application/octet-stream")
        response.setHeader("Content-disposition", "filename=${file.name}")
        response.outputStream << file.bytes
        return
    }
    }

In this i want to know which all are wrong.. I am getting error like url not found!

My Gsp:



On Sun, Aug 19, 2012 at 2:22 PM, Mikael Andersson <[hidden email]> wrote:
It is very difficult to answer questions about your code when we don't know how it works.

Is the project you are working on a Grails project where the data you are wanting to display is in a Grails domain object?

The code below is just an example using an example domain object where the primary key is "id" , a "name" field/column with what will be the default filename and where the file content field/column is "data". Using this information you should be able to apply this code example to your own domain model.

If you have difficulties please post code here showing what you are trying to do along with the error message you are getting.

- micke


On 19 August 2012 09:31, Saravanan S <[hidden email]> wrote:
Hi,
               If possible can you explain me this part of code. I dont understand where i am actually reading the data from database and how to specify the name of the file!!


def download() {
        def myObj = MyObj.find(params.id)
       
        //Whatever your content type is
        response.setContentType("text/
plain")
        response.setHeader("Content-disposition", "filename=${myObj.name}")
        response.outputStream << myObj.data
       
        return
    }


On Sun, Aug 19, 2012 at 1:27 PM, Mikael Andersson <[hidden email]> wrote:
There is loads about this on the internet, google "grails file download" and the first hit from stackoverflow shows you pretty much what you need to do.
It is always good if you google your problem and then try it out the suggested solutions. Then you can always include the code you've tried and get feed back on that from the mailing list.

Here is something to get you started.
You need to create an action which takes the primary key of the database record you want to return the file data for. Then you simply read your object from the DB and write the content you want to return to the output stream.


class FileController{

    def download() {
        def myObj = MyObj.find(params.id)
       
        //Whatever your content type is
        response.setContentType("text/plain")
        response.setHeader("Content-disposition", "filename=${myObj.name}")
        response.outputStream << myObj.data
       
        return
    }
}

<g:link controller="file" action="download" id="1"/>

In some old code of mine I also have (but this isn't needed) :

// add the next line to your file's imports part
 // import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
// the following obtains the instance of the GrailsWebRequest and disables standard view processing logic
def webRequest = request.getAttribute(GrailsApplicationAttributes.WEB_REQUEST)
 webRequest.setRenderView(false)

Can't quite remeber why I put this in and/or if it is needed (this code isn't used anymore). Wit the above I also flushed and closed the output stream.

Good luck,
 micke


On 19 August 2012 08:16, Saravanan S <[hidden email]> wrote:
Thanks!

Let me describe more precisely. I meant I have a Gsp page where i have 10 fields and save and clear button. When the user enters the details in the given field and clicks submit. The data's will be stored in the database. I am also displaying all the data's given by the user in the same page. My question is, i m able to display all the fields easily but the fileupload i cannot.   I mean like other field values i want to display the link of the file from database.

any help!!



On Sun, Aug 19, 2012 at 8:42 AM, Nathan Wells <[hidden email]> wrote:
Others may be expert enough to tell you exactly where your problem lies, but I'd need more details. For instance, can you be more specific about "i m not able to display the file link"? Have you verified the existence of the file on the file system? What errors do you get? Does the link render at all? Can we see the code where you're generating the link, along with appropriate descriptions of where the data is coming from to make the link?

Nathan Wells



On Sat, Aug 18, 2012 at 11:06 AM, Saravanan S <[hidden email]> wrote:
Hi all,
                  I have a question. In one of my project i developed a form where i get user details along with a text file upload option. In the same form i have a show option where the user will be able to view all the data's he just given. The problem is i m not able to display the file link. I want to display the file link from database and allow them to download by clicking on the link..

Any help is appreciated!!

Thanks!

--
Cheers,

Saravana





--
Cheers,

Saravana





--
Cheers,

Saravana





--
Cheers,

Saravana

Reply | Threaded
Open this post in threaded view
|

Re: Need help in displaying the text file which is already stored in the database!!

micke_
Hi

Looking at your ULearningUpload  domain object it looks like you want this in your download file action (changed to start with lowercase because that is the norm, not sure if that is why you got a 404.. could be):

def downloadFile = {
    // Something like this may be what you want
    def obj= ULearningUpload.read(params.id) // or ULearningUpload.findByLearningId(params.id)

    if (obj) {
        response.setContentType("application/octet-stream")
        response.setHeader("Content-disposition", "filename=file_with_id_${obj.learning_id}")
        response.outputStream << obj.learningUpload
        return
   }
}

Some observations :
- You probably want to change the toString() can beinvoked many times and you probably don't want the file content to be its value
- Perhaps you can have a better filename from the ULearning object? Didn't see a name in the ULearningUpload object
- Since you are getting 404's try specifiying the controller to make sure the correct one is called:
<g:link controller="???" action="downloadFile" id="${learningDomainInstance.id}">Download</g:link>
- Put some logging in the controller and make sure it gets called, then make sure it returns the right thing.

cheers,
 micke

On 19 August 2012 15:53, Saravanan S <[hidden email]> wrote:
Hi thanks,
                      Yes, your right. I am working on a grails project with Intellij Idea. And my domain class is
class ULearningUpload {

    static mapping = {
        table 'tuser_learning_upload'
        // version is set to false, because this isn't available by default for legacy databases
        version false
        id generator:'identity', column:'learning_upload_id'
        userID column: 'user_id'

        uLearning column: 'learning_id'
        learningUpload column: 'learningupload'

    }
    static belongsTo = ULearning


    ULearning uLearning

    Integer userID;

      byte[] learningUpload;

    Integer isActive
    Integer createdBy
    Date createdTimestamp
    Integer modifiedBy
    Date modifiedTimestamp
    static constraints = {
        userID(size: 0..25)
        isActive(nullable: true, max: 2147483647)
        createdBy(nullable: true, max: 2147483647)
        createdTimestamp(nullable: true)
        modifiedBy(nullable: true, max: 2147483647)
        modifiedTimestamp(nullable: true)
    }
    String toString() {
        return "${learningUpload }"
    }
}


Here the learningupload is what the field i used for uploading. In my Gsp i am creating a table for all fields like this..

 <td valign="top">${i+1}</td>

                                                        <td valign="top">${fieldValue(bean: learningDomainInstance.learningTypeCodeDomain, field: "learningType")}</td>

                                                        <td valign="top">${fieldValue(bean: learningDomainInstance, field: "learningTitle")}</td>


                                                        <td valign="top">${fieldValue(bean: learningDomainInstance, field: "learningDescription")}</td>

                                                        <td valign="top">${fieldValue(bean: learningDomainInstance, field: "learningInitiaterWords")}</td>

                                                        <td valign="top">${fieldValue(bean: learningDomainInstance, field: "learningSourceReference")}</td>


                                                    <td valign="top"><g:link action="DownloadFile" id="${learningDomainInstance.id}">                 </g:link></td>


                                                        <td valign="top"> <g:link controller="initiateLearning" action="DownloadFile">Download</g:link>  </td>

                                                        <td valign="top"><g:link action="initiateLearning" id="${learningDomainInstance.id}"><img src="${resource(dir:'/images', file:'edit.png')}"
                                                                                                                                                  style="vertical-align:middle;cursor:pointer "/></g:link></td>


Similarly. As helped by you, i created a download action method which looks like this. Declared inside the controller..


def DownloadFile = {
    def file = new File("C:\\Users\\Saravana\\Desktop\\mailbody.txt")

    if (file.exists()) {
        response.setContentType("application/octet-stream")
        response.setHeader("Content-disposition", "filename=${file.name}")
        response.outputStream << file.bytes
        return
    }
    }

In this i want to know which all are wrong.. I am getting error like url not found!

My Gsp:




On Sun, Aug 19, 2012 at 2:22 PM, Mikael Andersson <[hidden email]> wrote:
It is very difficult to answer questions about your code when we don't know how it works.

Is the project you are working on a Grails project where the data you are wanting to display is in a Grails domain object?

The code below is just an example using an example domain object where the primary key is "id" , a "name" field/column with what will be the default filename and where the file content field/column is "data". Using this information you should be able to apply this code example to your own domain model.

If you have difficulties please post code here showing what you are trying to do along with the error message you are getting.

- micke


On 19 August 2012 09:31, Saravanan S <[hidden email]> wrote:
Hi,
               If possible can you explain me this part of code. I dont understand where i am actually reading the data from database and how to specify the name of the file!!


def download() {
        def myObj = MyObj.find(params.id)
       
        //Whatever your content type is
        response.setContentType("text/
plain")
        response.setHeader("Content-disposition", "filename=${myObj.name}")
        response.outputStream << myObj.data
       
        return
    }


On Sun, Aug 19, 2012 at 1:27 PM, Mikael Andersson <[hidden email]> wrote:
There is loads about this on the internet, google "grails file download" and the first hit from stackoverflow shows you pretty much what you need to do.
It is always good if you google your problem and then try it out the suggested solutions. Then you can always include the code you've tried and get feed back on that from the mailing list.

Here is something to get you started.
You need to create an action which takes the primary key of the database record you want to return the file data for. Then you simply read your object from the DB and write the content you want to return to the output stream.


class FileController{

    def download() {
        def myObj = MyObj.find(params.id)
       
        //Whatever your content type is
        response.setContentType("text/plain")
        response.setHeader("Content-disposition", "filename=${myObj.name}")
        response.outputStream << myObj.data
       
        return
    }
}

<g:link controller="file" action="download" id="1"/>

In some old code of mine I also have (but this isn't needed) :

// add the next line to your file's imports part
 // import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
// the following obtains the instance of the GrailsWebRequest and disables standard view processing logic
def webRequest = request.getAttribute(GrailsApplicationAttributes.WEB_REQUEST)
 webRequest.setRenderView(false)

Can't quite remeber why I put this in and/or if it is needed (this code isn't used anymore). Wit the above I also flushed and closed the output stream.

Good luck,
 micke


On 19 August 2012 08:16, Saravanan S <[hidden email]> wrote:
Thanks!

Let me describe more precisely. I meant I have a Gsp page where i have 10 fields and save and clear button. When the user enters the details in the given field and clicks submit. The data's will be stored in the database. I am also displaying all the data's given by the user in the same page. My question is, i m able to display all the fields easily but the fileupload i cannot.   I mean like other field values i want to display the link of the file from database.

any help!!



On Sun, Aug 19, 2012 at 8:42 AM, Nathan Wells <[hidden email]> wrote:
Others may be expert enough to tell you exactly where your problem lies, but I'd need more details. For instance, can you be more specific about "i m not able to display the file link"? Have you verified the existence of the file on the file system? What errors do you get? Does the link render at all? Can we see the code where you're generating the link, along with appropriate descriptions of where the data is coming from to make the link?

Nathan Wells



On Sat, Aug 18, 2012 at 11:06 AM, Saravanan S <[hidden email]> wrote:
Hi all,
                  I have a question. In one of my project i developed a form where i get user details along with a text file upload option. In the same form i have a show option where the user will be able to view all the data's he just given. The problem is i m not able to display the file link. I want to display the file link from database and allow them to download by clicking on the link..

Any help is appreciated!!

Thanks!

--
Cheers,

Saravana





--
Cheers,

Saravana





--
Cheers,

Saravana





--
Cheers,

Saravana


Reply | Threaded
Open this post in threaded view
|

Re: Need help in displaying the text file which is already stored in the database!!

saravanataee
Hi,
             Thank you very much! I finally got your point and it works fine!!


On Mon, Aug 20, 2012 at 12:03 AM, Mikael Andersson <[hidden email]> wrote:
Hi

Looking at your ULearningUpload  domain object it looks like you want this in your download file action (changed to start with lowercase because that is the norm, not sure if that is why you got a 404.. could be):

def downloadFile = {
    // Something like this may be what you want
    def obj= ULearningUpload.read(params.id) // or ULearningUpload.findByLearningId(params.id)

    if (obj) {
        response.setContentType("application/octet-stream")
        response.setHeader("Content-disposition", "filename=file_with_id_${obj.learning_id}")
        response.outputStream << obj.learningUpload
        return
   }
}

Some observations :
- You probably want to change the toString() can beinvoked many times and you probably don't want the file content to be its value
- Perhaps you can have a better filename from the ULearning object? Didn't see a name in the ULearningUpload object
- Since you are getting 404's try specifiying the controller to make sure the correct one is called:
<g:link controller="???" action="downloadFile" id="${learningDomainInstance.id}">Download</g:link>
- Put some logging in the controller and make sure it gets called, then make sure it returns the right thing.

cheers,
 micke


On 19 August 2012 15:53, Saravanan S <[hidden email]> wrote:
Hi thanks,
                      Yes, your right. I am working on a grails project with Intellij Idea. And my domain class is
class ULearningUpload {

    static mapping = {
        table 'tuser_learning_upload'
        // version is set to false, because this isn't available by default for legacy databases
        version false
        id generator:'identity', column:'learning_upload_id'
        userID column: 'user_id'

        uLearning column: 'learning_id'
        learningUpload column: 'learningupload'

    }
    static belongsTo = ULearning


    ULearning uLearning

    Integer userID;

      byte[] learningUpload;

    Integer isActive
    Integer createdBy
    Date createdTimestamp
    Integer modifiedBy
    Date modifiedTimestamp
    static constraints = {
        userID(size: 0..25)
        isActive(nullable: true, max: <a href="tel:2147483647" value="+12147483647" target="_blank">2147483647)
        createdBy(nullable: true, max: <a href="tel:2147483647" value="+12147483647" target="_blank">2147483647)
        createdTimestamp(nullable: true)
        modifiedBy(nullable: true, max: <a href="tel:2147483647" value="+12147483647" target="_blank">2147483647)
        modifiedTimestamp(nullable: true)
    }
    String toString() {
        return "${learningUpload }"
    }
}


Here the learningupload is what the field i used for uploading. In my Gsp i am creating a table for all fields like this..

 <td valign="top">${i+1}</td>

                                                        <td valign="top">${fieldValue(bean: learningDomainInstance.learningTypeCodeDomain, field: "learningType")}</td>

                                                        <td valign="top">${fieldValue(bean: learningDomainInstance, field: "learningTitle")}</td>


                                                        <td valign="top">${fieldValue(bean: learningDomainInstance, field: "learningDescription")}</td>

                                                        <td valign="top">${fieldValue(bean: learningDomainInstance, field: "learningInitiaterWords")}</td>

                                                        <td valign="top">${fieldValue(bean: learningDomainInstance, field: "learningSourceReference")}</td>


                                                    <td valign="top"><g:link action="DownloadFile" id="${learningDomainInstance.id}">                 </g:link></td>


                                                        <td valign="top"> <g:link controller="initiateLearning" action="DownloadFile">Download</g:link>  </td>

                                                        <td valign="top"><g:link action="initiateLearning" id="${learningDomainInstance.id}"><img src="${resource(dir:'/images', file:'edit.png')}"
                                                                                                                                                  style="vertical-align:middle;cursor:pointer "/></g:link></td>


Similarly. As helped by you, i created a download action method which looks like this. Declared inside the controller..


def DownloadFile = {
    def file = new File("C:\\Users\\Saravana\\Desktop\\mailbody.txt")

    if (file.exists()) {
        response.setContentType("application/octet-stream")
        response.setHeader("Content-disposition", "filename=${file.name}")
        response.outputStream << file.bytes
        return
    }
    }

In this i want to know which all are wrong.. I am getting error like url not found!

My Gsp:




On Sun, Aug 19, 2012 at 2:22 PM, Mikael Andersson <[hidden email]> wrote:
It is very difficult to answer questions about your code when we don't know how it works.

Is the project you are working on a Grails project where the data you are wanting to display is in a Grails domain object?

The code below is just an example using an example domain object where the primary key is "id" , a "name" field/column with what will be the default filename and where the file content field/column is "data". Using this information you should be able to apply this code example to your own domain model.

If you have difficulties please post code here showing what you are trying to do along with the error message you are getting.

- micke


On 19 August 2012 09:31, Saravanan S <[hidden email]> wrote:
Hi,
               If possible can you explain me this part of code. I dont understand where i am actually reading the data from database and how to specify the name of the file!!


def download() {
        def myObj = MyObj.find(params.id)
       
        //Whatever your content type is
        response.setContentType("text/
plain")
        response.setHeader("Content-disposition", "filename=${myObj.name}")
        response.outputStream << myObj.data
       
        return
    }


On Sun, Aug 19, 2012 at 1:27 PM, Mikael Andersson <[hidden email]> wrote:
There is loads about this on the internet, google "grails file download" and the first hit from stackoverflow shows you pretty much what you need to do.
It is always good if you google your problem and then try it out the suggested solutions. Then you can always include the code you've tried and get feed back on that from the mailing list.

Here is something to get you started.
You need to create an action which takes the primary key of the database record you want to return the file data for. Then you simply read your object from the DB and write the content you want to return to the output stream.


class FileController{

    def download() {
        def myObj = MyObj.find(params.id)
       
        //Whatever your content type is
        response.setContentType("text/plain")
        response.setHeader("Content-disposition", "filename=${myObj.name}")
        response.outputStream << myObj.data
       
        return
    }
}

<g:link controller="file" action="download" id="1"/>

In some old code of mine I also have (but this isn't needed) :

// add the next line to your file's imports part
 // import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
// the following obtains the instance of the GrailsWebRequest and disables standard view processing logic
def webRequest = request.getAttribute(GrailsApplicationAttributes.WEB_REQUEST)
 webRequest.setRenderView(false)

Can't quite remeber why I put this in and/or if it is needed (this code isn't used anymore). Wit the above I also flushed and closed the output stream.

Good luck,
 micke


On 19 August 2012 08:16, Saravanan S <[hidden email]> wrote:
Thanks!

Let me describe more precisely. I meant I have a Gsp page where i have 10 fields and save and clear button. When the user enters the details in the given field and clicks submit. The data's will be stored in the database. I am also displaying all the data's given by the user in the same page. My question is, i m able to display all the fields easily but the fileupload i cannot.   I mean like other field values i want to display the link of the file from database.

any help!!



On Sun, Aug 19, 2012 at 8:42 AM, Nathan Wells <[hidden email]> wrote:
Others may be expert enough to tell you exactly where your problem lies, but I'd need more details. For instance, can you be more specific about "i m not able to display the file link"? Have you verified the existence of the file on the file system? What errors do you get? Does the link render at all? Can we see the code where you're generating the link, along with appropriate descriptions of where the data is coming from to make the link?

Nathan Wells



On Sat, Aug 18, 2012 at 11:06 AM, Saravanan S <[hidden email]> wrote:
Hi all,
                  I have a question. In one of my project i developed a form where i get user details along with a text file upload option. In the same form i have a show option where the user will be able to view all the data's he just given. The problem is i m not able to display the file link. I want to display the file link from database and allow them to download by clicking on the link..

Any help is appreciated!!

Thanks!

--
Cheers,

Saravana





--
Cheers,

Saravana





--
Cheers,

Saravana





--
Cheers,

Saravana





--
Cheers,

Saravana