[TIMOB-4311] Android: Can't set Content-Type request header
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Medium |
Status | Closed |
Resolution | Won't Fix |
Resolution Date | 2011-06-28T21:04:10.000+0000 |
Affected Version/s | Release 1.7.0 |
Fix Version/s | Sprint 2011-26 |
Components | Android |
Labels | n/a |
Reporter | Jon Alter |
Assignee | Opie Cyrus |
Created | 2011-06-03T10:27:13.000+0000 |
Updated | 2017-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!";
}
?>
Closing ticket as the issue will not fix.