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?
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"; } ?>