Quantcast

upload file(image) and add it to database

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

upload file(image) and add it to database

chichibek bros
Hi group, i dont lie if a say that before come here asking for help i tried to find and answer.

i followed this documentation http://grails.org/doc/latest/guide/theWebLayer.html#uploadingFiles specifically here File Uploads through Data Binding,
because this is what i want, because for me this solution looks easier in the situation when i could need to migrate to another database now im using the default database, i want to save an image into the database later show it in an img tag

but this recomendations are not working from me. do you have any suggestion? 

from now thanks for your help!

 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: upload file(image) and add it to database

didinj
Have you try this http://grails.org/Simple+Avatar+Uploader

2012/6/22 chichibek bros [via Grails] <[hidden email]>
Hi group, i dont lie if a say that before come here asking for help i tried to find and answer.

i followed this documentation http://grails.org/doc/latest/guide/theWebLayer.html#uploadingFiles specifically here File Uploads through Data Binding,
because this is what i want, because for me this solution looks easier in the situation when i could need to migrate to another database now im using the default database, i want to save an image into the database later show it in an img tag

but this recomendations are not working from me. do you have any suggestion? 

from now thanks for your help!

 



If you reply to this email, your message will be added to the discussion below:
http://grails.1312388.n4.nabble.com/upload-file-image-and-add-it-to-database-tp4630532.html
To unsubscribe from Grails, click here.
NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: upload file(image) and add it to database

ideasculptor
In reply to this post by chichibek bros


On Thu, Jun 21, 2012 at 5:43 PM, chichibek bros <[hidden email]> wrote:
Hi group, i dont lie if a say that before come here asking for help i tried to find and answer.

i followed this documentation http://grails.org/doc/latest/guide/theWebLayer.html#uploadingFiles specifically here File Uploads through Data Binding,
because this is what i want, because for me this solution looks easier in the situation when i could need to migrate to another database now im using the default database, i want to save an image into the database later show it in an img tag

but this recomendations are not working from me. do you have any suggestion? 

What, specifically, isn't working for you?


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: upload file(image) and add it to database

0coco0
Hello!

I would not recommand saving the image directly in the database, but in file system. You will only save the path to the image in the database. Let me know what you need exactly, maybe I can help.

Regards

On Fri, Jun 22, 2012 at 1:31 PM, Samuel Gendler <[hidden email]> wrote:


On Thu, Jun 21, 2012 at 5:43 PM, chichibek bros <[hidden email]> wrote:
Hi group, i dont lie if a say that before come here asking for help i tried to find and answer.

i followed this documentation http://grails.org/doc/latest/guide/theWebLayer.html#uploadingFiles specifically here File Uploads through Data Binding,
because this is what i want, because for me this solution looks easier in the situation when i could need to migrate to another database now im using the default database, i want to save an image into the database later show it in an img tag

but this recomendations are not working from me. do you have any suggestion? 

What, specifically, isn't working for you?



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: upload file(image) and add it to database

Miles Burton-2
You could use a blob (image to a byte[] in a domain object), I've seen
that done before but I'd highly recommend against it!

On Fri, Jun 22, 2012 at 3:03 PM, reda khyatti <[hidden email]> wrote:

> Hello!
>
> I would not recommand saving the image directly in the database, but in file
> system. You will only save the path to the image in the database. Let me
> know what you need exactly, maybe I can help.
>
> Regards
>
>
> On Fri, Jun 22, 2012 at 1:31 PM, Samuel Gendler <[hidden email]>
> wrote:
>>
>>
>>
>> On Thu, Jun 21, 2012 at 5:43 PM, chichibek bros <[hidden email]>
>> wrote:
>>>
>>> Hi group, i dont lie if a say that before come here asking for help i
>>> tried to find and answer.
>>>
>>> i followed this
>>> documentation http://grails.org/doc/latest/guide/theWebLayer.html#uploadingFiles specifically here
>>> File Uploads through Data Binding,
>>> because this is what i want, because for me this solution looks easier in
>>> the situation when i could need to migrate to another database now im using
>>> the default database, i want to save an image into the database later show
>>> it in an img tag
>>>
>>> but this recomendations are not working from me. do you have any
>>> suggestion?
>>
>>
>> What, specifically, isn't working for you?
>>
>>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: upload file(image) and add it to database

jphiloon
In reply to this post by chichibek bros
No idea what your error is, but for byte[ ] blobs try adding this to your domain class:
static mapping =  {
    blobColumn type:'org.hibernate.type.MaterializedBlobType'
    ....
}

It was necessary for me to do this for an Oracle db; not sure about other dbs.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: upload file(image) and add it to database

ideasculptor
In reply to this post by 0coco0


On Fri, Jun 22, 2012 at 7:03 AM, reda khyatti <[hidden email]> wrote:
Hello!

I would not recommand saving the image directly in the database, but in file system. You will only save the path to the image in the database. Let me know what you need exactly, maybe I can help.

That's really a context-sensitive decision.  It makes a lot of sense to store the image data in the database itself for some applications.  It depends on how efficiently your database can handle it, and the nature of your application - It can be difficult to distribute image files to a large cluster of webservers such that an fs path is consistent, backups of the image dir(s) have to be kept in sync with backups of the database so that db restores will have consistent image data, any non-webserver hosts which also use the database and need access to the images have to have filesystem-level access to the images, etc.  Yes, in most cases, it is pretty easy to make an argument for storing image data externally to the database, but it isn't at all difficult to think of circumstances where that wouldn't be true.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: upload file(image) and add it to database

chichibek bros
In reply to this post by jphiloon
thanks for answer, i did not give you enough information, i going to paste the structure and the error message

domain
- - - - - - -
package biblio

class Library {

    String name
    String slogan
    String address
    byte [] logo
    Date dateCreated

    static constraints = {
        name(unique:true,blank:false)
        slogan(nullable:true)
        address(nullable:true)
        logo(nullable:true)
    }

    static mapping = {
        logo column:'logo', sqltype:'blob'
    }

    String toString() {
        name
    }

    static hasMany = [user:User]
}

gsp
- - - - - -

<g:uploadForm action="updateImage">
        <g:hiddenField name="id" value="${params.id}"/>
        <input type="file" name="logo">
        <br>
        <g:submitButton name="send" value="${message(code:'biblio.upload')}"/>
</g:uploadForm>

controller
- - - - - -

def updateImage() {
          //def libraryInstance = new Library(params)
          def libraryInstance = Library.get(params.id)
          libraryInstance.properties['logo'] = params.logo

          flash.message = (libraryInstance.save(flush:true)) ? message(code:'biblio.success') : message(code:'biblio.error')

          if(library.hasErrors()) {
              println library.errors
          }

          redirect(action:'image')
}


error message
- - - - - - - - -
URI
/biblio/library/updateImage
Class
org.codehaus.groovy.runtime.typehandling.GroovyCastException
Message
Cannot cast object 'org.springframework.web.multipart.commons.CommonsMultipartFile@185a7c1' with class 'org.springframework.web.multipart.commons.CommonsMultipartFile' to class 'byte'


thanks again.


2012/6/22 jphiloon <[hidden email]>
No idea what your error is, but for byte[ ] blobs try adding this to your
domain class:
static mapping =  {
   blobColumn type:'org.hibernate.type.MaterializedBlobType'
   ....
}

It was necessary for me to do this for an Oracle db; not sure about other
dbs.



--
View this message in context: http://grails.1312388.n4.nabble.com/upload-file-image-and-add-it-to-database-tp4630532p4630565.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: upload file(image) and add it to database

Nathan Wells
In reply to this post by ideasculptor
I think, in most cases, it is easier to solve the distributed file system problem than the problems inherent in binary data stored in the database.

All decisions are context-sensitive, but following general principles can help you make the right decision in many contexts. My opinion of the general case is that there are many people who have worked hard on behalf of many more people to make a network-shared storage system work. There are less people who've worked on making your particular database implementation capable of storing images effectively.

In general, many developers agree that storing images outside of the database is the best practice unless you have some really good reason not to do it this way. I've never really heard a good reason.

Nathan Wells


On Fri, Jun 22, 2012 at 11:54 AM, Samuel Gendler <[hidden email]> wrote:


On Fri, Jun 22, 2012 at 7:03 AM, reda khyatti <[hidden email]> wrote:
Hello!

I would not recommand saving the image directly in the database, but in file system. You will only save the path to the image in the database. Let me know what you need exactly, maybe I can help.

That's really a context-sensitive decision.  It makes a lot of sense to store the image data in the database itself for some applications.  It depends on how efficiently your database can handle it, and the nature of your application - It can be difficult to distribute image files to a large cluster of webservers such that an fs path is consistent, backups of the image dir(s) have to be kept in sync with backups of the database so that db restores will have consistent image data, any non-webserver hosts which also use the database and need access to the images have to have filesystem-level access to the images, etc.  Yes, in most cases, it is pretty easy to make an argument for storing image data externally to the database, but it isn't at all difficult to think of circumstances where that wouldn't be true.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: upload file(image) and add it to database

wilsonaikeda@gmail.com
In reply to this post by chichibek bros
You need to put : enctype="multipart/form-data" in your form declaration

On Jun 23, 2012, at 2:59 AM, chichibek bros wrote:

thanks for answer, i did not give you enough information, i going to paste the structure and the error message

domain
- - - - - - -
package biblio

class Library {

    String name
    String slogan
    String address
    byte [] logo
    Date dateCreated

    static constraints = {
        name(unique:true,blank:false)
        slogan(nullable:true)
        address(nullable:true)
        logo(nullable:true)
    }

    static mapping = {
        logo column:'logo', sqltype:'blob'
    }

    String toString() {
        name
    }

    static hasMany = [user:User]
}

gsp
- - - - - -

<g:uploadForm action="updateImage">
        <g:hiddenField name="id" value="${params.id}"/>
        <input type="file" name="logo">
        <br>
        <g:submitButton name="send" value="${message(code:'biblio.upload')}"/>
</g:uploadForm>

controller
- - - - - -

def updateImage() {
          //def libraryInstance = new Library(params)
          def libraryInstance = Library.get(params.id)
          libraryInstance.properties['logo'] = params.logo

          flash.message = (libraryInstance.save(flush:true)) ? message(code:'biblio.success') : message(code:'biblio.error')

          if(library.hasErrors()) {
              println library.errors
          }

          redirect(action:'image')
}


error message
- - - - - - - - -
URI
/biblio/library/updateImage
Class
org.codehaus.groovy.runtime.typehandling.GroovyCastException
Message
Cannot cast object 'org.springframework.web.multipart.commons.CommonsMultipartFile@185a7c1' with class 'org.springframework.web.multipart.commons.CommonsMultipartFile' to class 'byte'


thanks again.


2012/6/22 jphiloon <[hidden email]>
No idea what your error is, but for byte[ ] blobs try adding this to your
domain class:
static mapping =  {
   blobColumn type:'org.hibernate.type.MaterializedBlobType'
   ....
}

It was necessary for me to do this for an Oracle db; not sure about other
dbs.



--
View this message in context: http://grails.1312388.n4.nabble.com/upload-file-image-and-add-it-to-database-tp4630532p4630565.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email




Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: upload file(image) and add it to database

chichibek bros
thanks you all for your help the code is now working, i followed didinj recomendation of using http://grails.org/Simple+Avatar+Uploader. Thanks

2012/6/22 Akira Ikeda <[hidden email]>
You need to put : enctype="multipart/form-data" in your form declaration

On Jun 23, 2012, at 2:59 AM, chichibek bros wrote:

thanks for answer, i did not give you enough information, i going to paste the structure and the error message

domain
- - - - - - -
package biblio

class Library {

    String name
    String slogan
    String address
    byte [] logo
    Date dateCreated

    static constraints = {
        name(unique:true,blank:false)
        slogan(nullable:true)
        address(nullable:true)
        logo(nullable:true)
    }

    static mapping = {
        logo column:'logo', sqltype:'blob'
    }

    String toString() {
        name
    }

    static hasMany = [user:User]
}

gsp
- - - - - -

<g:uploadForm action="updateImage">
        <g:hiddenField name="id" value="${params.id}"/>
        <input type="file" name="logo">
        <br>
        <g:submitButton name="send" value="${message(code:'biblio.upload')}"/>
</g:uploadForm>

controller
- - - - - -

def updateImage() {
          //def libraryInstance = new Library(params)
          def libraryInstance = Library.get(params.id)
          libraryInstance.properties['logo'] = params.logo

          flash.message = (libraryInstance.save(flush:true)) ? message(code:'biblio.success') : message(code:'biblio.error')

          if(library.hasErrors()) {
              println library.errors
          }

          redirect(action:'image')
}


error message
- - - - - - - - -
URI
/biblio/library/updateImage
Class
org.codehaus.groovy.runtime.typehandling.GroovyCastException
Message
Cannot cast object 'org.springframework.web.multipart.commons.CommonsMultipartFile@185a7c1' with class 'org.springframework.web.multipart.commons.CommonsMultipartFile' to class 'byte'


thanks again.


2012/6/22 jphiloon <[hidden email]>
No idea what your error is, but for byte[ ] blobs try adding this to your
domain class:
static mapping =  {
   blobColumn type:'org.hibernate.type.MaterializedBlobType'
   ....
}

It was necessary for me to do this for an Oracle db; not sure about other
dbs.



--
View this message in context: http://grails.1312388.n4.nabble.com/upload-file-image-and-add-it-to-database-tp4630532p4630565.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email





Loading...