Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-4311] Android: Can't set Content-Type request header

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionWon't Fix
Resolution Date2011-06-28T21:04:10.000+0000
Affected Version/sRelease 1.7.0
Fix Version/sSprint 2011-26
ComponentsAndroid
Labelsn/a
ReporterJon Alter
AssigneeOpie Cyrus
Created2011-06-03T10:27:13.000+0000
Updated2017-03-16T22:02:04.000+0000

Description

When you set Content-Type, it gets into the xhr.onload, but the responseText is "There was an error uploading the file, please try again!" and the file does not upload. If you remove "xhr.setRequestHeader("Content-Type", "image/jpeg");" then it works fine and uploads the picture. Step 1: put uploads.php somewhere that you can run it and make an 'uploads' folder in the same directory Step 2: change the url in app.js to point to uploads.php. Step 3: run the example app.js code. Step 4: click 'upload' Step 5: select a picture Step 6: wait for response from the server.
var win = Ti.UI.createWindow({
    backgroundColor: 'white'
});

var button = Ti.UI.createButton({
	title:'upload',
	height: 50,
	width: 200
});
button.addEventListener('click', function(e){
	Titanium.Media.openPhotoGallery({
		success:function(event){
			var myImage = event.media;
			
			var sendImageData = { 
					        "uploadedfile": myImage
					    };

		    var xhr = Titanium.Network.createHTTPClient();
		    xhr.open("POST","http://_________/uploads.php");
		    xhr.setRequestHeader("enctype", "multipart/form-data");
		    xhr.setRequestHeader("Content-Type", "image/jpeg");
		    xhr.onload = function(e){
				Ti.UI.createAlertDialog({title:'Success', message: this.responseText
			}).show();};
		    xhr.onerror = function(e){alert(e.error);};
		    xhr.send(sendImageData);
		},
		cancel:function(){

		},
		error:function(error){
		},
		mediaTypes:[Ti.Media.MEDIA_TYPE_VIDEO,Ti.Media.MEDIA_TYPE_PHOTO]
	});
});

win.add(button);
win.open();
<?php

$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name'])." has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

?>

Trace

http://pastie.org/private/4ne5vkfv5lc78vtklchg

Tested On

TiSDK 1.7.0.RC1 Android Emulator 2.1

Associated Helpdesk Ticket

http://appc.me/c/APP-152932

Comments

  1. Lee Morris 2017-03-16

    Closing ticket as the issue will not fix.

JSON Source