2fas2c
Posts: 0
Joined: Mon Dec 16, 2013 11:43 pm

Uploading an image as part of user registration

I have seen several articles regarding uploading images. Sorry for asking it again. I have a user registration form on which I want the user to upload their picture/avatar. Can I do it all in one form? Can I have a field on the user object (photo with a data type of String)? Do I need to base64 encode before storing it in this column?

Thanks

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

Uploading an image as part of user registration

Hi --

You can do it with Appery.io.

So if you want to send file content into user collection you should:

  1. Create in form textarea field. And map it into useravatar DB field.

  2. Add "select file" button as it described in tutorial: http://docs.appery.io/tutorials/uploa...

  3. Change reader code from:
    pre
    var reader = new FileReader();
    reader.onloadend = function(e) {
    var fileContent = $('[name=fileContent]');
    fileContent.text(e.target.result);
    };
    reader.readAsText(file);
    /pre
    to
    pre
    var reader = new FileReader();
    reader.onloadend = function(e) {

    Code: Select all

     //Getting base64 string 
     var base64String = e.target.result; 
    
     //Setting base64 as asset for image. (you can even see selected image immediatly in your UI). 
     Appery("image_result").attr("src", base64String); 
    
     //Settings content of file to your textArea. 
     Appery("yourTextArea").val(base64String); 

};
//This is function used by you. Comment it.
//reader.readAsText(file);

Code: Select all

 //You should use this one. 
 reader.readAsDataURL(file); 

/pre

That's all.

Return to “Issues”