Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-9590] Android: Using HTTPClient to PUT an image on Android uploads 15 bytes of text

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2014-08-11T17:46:05.000+0000
Affected Version/sRelease 2.0.2, Release 2.0.1
Fix Version/sSprint 2012-15 Core, Release 2.1.2, Release 3.0.0
ComponentsAndroid
LabelsSupportTeam, core, module_network, parity, qe-manualtest, qe-port
ReporterNikhil Sharma
AssigneeMax Stepanov
Created2012-06-15T21:54:09.000+0000
Updated2014-08-11T17:56:13.000+0000

Description

When I run the below code on Android, the image is not uploaded, rather a 15 byte text file with the following text: [object TiBlob] The code below runs fine on iOS, the image uploads successfully.

Repo Steps

1. Run the given code sample in your app.js 2. Click upload image button. 3. It will then display the URL to the uploaded image. 4. For Android, you have to load the link in a browser and Save as. 5. Then change the extension to .txt to see its just a text file with the [object TiBlob] text.
var win1 = Titanium.UI.createWindow({  
    title:'Win 1',
    backgroundColor:'#fff'
});

var img = Ti.UI.createImageView({
	image: 'http://www.guestassist.net/images/logo.png',
	width: 229,
	height: 66,
	top: 20
});

var b1 = Ti.UI.createButton({
	title: 'upload image',
	width: 200,
	height: 40,
	top: 100
});

b1.addEventListener('click', uploadImage);

var link = Ti.UI.createTextField({
	value: '(url will show here)',
	top: 160,
	left: 10,
	right: 10,
	height: 60,
	backgroundColor: 'white',
	borderColor: '#dddddd',
	borderWidth: 1,
	font: {
		fontSize: 13
	}
});

win1.add(img);
win1.add(b1);
win1.add(link);
win1.open();

function uploadImage()
{
	var http = Ti.Network.createHTTPClient({
		onerror: function(e) {
			if (JSON.stringify(e).search('The request timed out') >= 0)
			{
				Ti.API.info('timeout');
			}
			else
			{
				Ti.API.info('error');
			}
		},
		onload: function(e) {
			Ti.API.info('success!');
		},
		enableKeepAlive: false,
		timeout: 360000
	});
	
	
	Ti.App.idleTimerDisabled = true;
	var d = new Date();
	var raw = d.getTime();
	var filename = raw + '.png';
	
	var f = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory + '/' + filename);
	f.write(img.toBlob());
	
	var uploadFile = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory, filename);
	if (!uploadFile.exists()) { alert('file not found'); return false; }
	var fileContents = uploadFile.read();
	var curDate = 'Wed, 19 June 2011 10:00:00 -0500'; // hard coding date in AWS format for testing
	var fileURL = 'https://s3.amazonaws.com/ga-testbucket/images/' + filename;
			
	http.open('PUT', fileURL, false);
	http.setRequestHeader('Content-Type', fileContents.mimeType);
	if (Ti.Platform.osname != 'android')
	{
		http.setRequestHeader('Content-Length', uploadFile.size);
	}
	http.setRequestHeader('Host', 's3.amazonaws.com');
	http.setRequestHeader('Date', curDate);
	http.setRequestHeader('x-amz-acl', 'public-read');
	http.send(fileContents);
	
	alert('image uploaded!');
	link.value = fileURL;
}

Comments

  1. Mark Ross 2012-06-16

    There is a forum discussion about this issue as well. Although not the original poster's issue, the Android PUT issue is discussed in the comments: http://developer.appcelerator.com/question/120366/rest-put-to-aws-s3----works-on-ios-not-android
  2. Max Stepanov 2012-07-19

    Test case #2 (sends TiFile instead of TiBlob):
       var win1 = Titanium.UI.createWindow({  
           title:'Win 1',
           backgroundColor:'#fff'
       });
        
       var img = Ti.UI.createImageView({
           image: 'http://www.guestassist.net/images/logo.png',
           width: 229,
           height: 66,
           top: 20
       });
        
       var b1 = Ti.UI.createButton({
           title: 'upload image',
           width: 200,
           height: 40,
           top: 100
       });
        
       b1.addEventListener('click', uploadImage);
        
       var link = Ti.UI.createTextField({
           value: '(url will show here)',
           top: 160,
           left: 10,
           right: 10,
           height: 60,
           backgroundColor: 'white',
           borderColor: '#dddddd',
           borderWidth: 1,
           font: {
               fontSize: 13
           }
       });
        
       win1.add(img);
       win1.add(b1);
       win1.add(link);
       win1.open();
        
       function uploadImage()
       {
           var http = Ti.Network.createHTTPClient({
               onerror: function(e) {
                   if (JSON.stringify(e).search('The request timed out') >= 0)
                   {
                       Ti.API.info('timeout');
                   }
                   else
                   {
                       Ti.API.info('error');
                   }
               },
               onload: function(e) {
                   Ti.API.info('success!');
               },
               enableKeepAlive: false,
               timeout: 360000
           });
            
            
           Ti.App.idleTimerDisabled = true;
           var d = new Date();
           var raw = d.getTime();
           var filename = raw + '.png';
            
           var f = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory + '/' + filename);
           f.write(img.toBlob());
            
           var uploadFile = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory, filename);
           if (!uploadFile.exists()) { alert('file not found'); return false; }
           var fileContents = uploadFile.read();
           var curDate = 'Wed, 19 June 2011 10:00:00 -0500'; // hard coding date in AWS format for testing
           var fileURL = 'https://s3.amazonaws.com/ga-testbucket/images/' + filename;
                    
           http.open('PUT', fileURL, false);
           http.setRequestHeader('Content-Type', fileContents.mimeType);
           if (Ti.Platform.osname != 'android')
           {
               http.setRequestHeader('Content-Length', uploadFile.size);
           }
           http.setRequestHeader('Host', 's3.amazonaws.com');
           http.setRequestHeader('Date', curDate);
           http.setRequestHeader('x-amz-acl', 'public-read');
           http.send(uploadFile); // TiFile
            
           alert('image uploaded!');
           link.value = fileURL;
       }
       
  3. Max Stepanov 2012-07-19

    PR pending https://github.com/appcelerator/titanium_mobile/pull/2607
  4. Max Stepanov 2012-08-13

    Backport PR https://github.com/appcelerator/titanium_mobile/pull/2736
  5. Sabil Rahim 2012-08-14

    Reopening to update FixVersion 2.1.2
  6. Tamila Smolich 2012-08-20

    Closing as fixed. Tested and verified on: Titanium Studio, build: 2.1.1.201208091713 Titanium SDK, build: 2.1.2.v20120816171609 Devices: Nexus 7 tab (4.1.1)

JSON Source