Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Manipulating a TiBlob

I am getting a blob from Ti.Filesystem.File.read() that is a png file I've read in.
I need to base64 encode this and send it to my server.
Calling Ti.Utils.base64encode( image ) gives me

Invalid type passed to function. expected: TiBlob, was: NSString in -[UtilsModule base64encode:] (UtilsModule.m:22)

changing this to Ti.Utils.base64encode( image.text ) gives me

Invalid type passed to function. expected: NSNull, was: NSString in -[UtilsModule base64encode:] (UtilsModule.m:22)

are there any tools that let me manipulate the image blob into something I can work with?

— asked May 27th 2010 by Allen Firstenberg
  • base64encode
  • blob
  • file
  • filesystem
  • iphone
  • mobile
  • tiblob
0 Comments

1 Answer

  • Hello to whomever finds this reply. For the record, I think it's shameful that nobody answered this question after 2 years, considering how easy this is to solve. Here is some code that I've used:

    var global = {
        DATA_PATH: Ti.Filesystem.applicationDataDirectory
    };
    
    function sendFile(filename, contentType, serverURL) {
        var fileObject = Ti.Filesystem.getFile(global.DATA_PATH,filename);
        if (fileObject.exists()) {
            var data = {
                name: filename,
                file: fileObject.read()
            };
            xhr = Ti.Network.createHTTPClient();
            xhr.setRequestHeader("enctype", "multipart/form-data");
            xhr.setRequestHeader("Content-Type", contentType);
            xhr.open("POST",serverURL);
            xhr.send(data); 
            xhr.onload = function() {
                var response = this.responseText.toString();
                Ti.API.info("Upload response: " + response);
                if (response.indexOf("Success") != -1) {
                    /*
                        Do whatever you need with the response from the server
                    */
                } else {
                    alert("Error occurred on upload.");
                }
            };
        }
    }
    

    To send a PNG file, call the above like this:

    sendFile('myimage.png','image/png','http://www.myserver.com/upload.php');
    

    Just make sure that the 'contentType' argument matches the type of file you're sending.

    On the server end, you can use the following. This PHP code simply stores the received file in an "uploads" directory that is located just beneath the directory containing this script:

    <?php
    //*** upload.php
    $uploaddir = './uploads/';
    //echo var_dump($_FILES);
    
    $file = $uploaddir . basename($_FILES['file']['name']); 
    
    if (move_uploaded_file($_FILES['file']['tmp_name'], $file)) { 
        echo "Success"; 
    } else {
        echo "Error";
    }
    ?>
    
    — answered November 29th 2011 by Mark Pemburn
    permalink
    2 Comments
    • Thanks for the reply, I haven't personally used it but I may in the near future, if I do I'll post my experience to your solution.

      I also agree that it's shameful that something had never been answered to responded to for 2 years. I've seen this occurring on other posts in the Appcelerator forums. as much as Titanium is a great product with some great vision but it risks becoming just another framework with limited support unless you fork out quite a bit of cash. That to me is not a good foundation to a product or framework.

      — commented April 2nd 2012 by Sam Shoghi
    • Hi!
      Thank you for your answer, i'm a newbie so, thanks for all the tutorials and info that you people are posting, because it helps me A LOT!!.

      What i would like to know is if its possible to convert an image to a base64 string with the Ti.Utils.base64encode(image)function.

      Here is my example:

      var f = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'images/logo.png'); //This is a lgo that i'm using to test
      
      var encoded = Ti.Utils.base64encode(f.read()); //This gives em a Ti.Blob
      //But how do i obtain the string??  decoded.text is null so....
      
      var decodedData = Titanium.Utils.base64decode(res.result.Foto);//this is a foto that i have downloaded from the server (theorically is a base64encoded string)
      //So in decodedData now i would have a titanium.blob
      
      var imageGal = Titanium.UI.createImageView();
      
      imageGal.image = decodeData;// in the imageGal an image should be shown, isn't it?
      

      So what i'm doing wrong? any help?

      Thank you!!!!

      — commented April 3rd 2012 by Luis Ramos
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.