Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-9590

Android: Using HTTPClient to PUT an image on Android uploads 15 bytes of text

    Details

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

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                mstepanov Max Stepanov
                Reporter:
                nsharma Nikhil Sharma
              • Watchers:
                0 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved:

                  Backbone Issue Sync

                  • Titanium SDK/CLI <> Titanium Mobile
                    Synced with:
                    TIMOB-13738
                    Sync status:
                    ERROR
                    Last received:
                    Last sent:

                    Git Source Code