Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Contributor
emsbenitez
Posts: 15
Registered: ‎05-19-2011
My Device: Storm2 9550
My Carrier: Student

Use blobToString() to upload Binary File to a server

Hi! I'm trying to do something interesting. I wanna upload a file using AJAX. 

 

When I press the button to send the form I have the next function

 

function sendForm(){

var filePath = document.getElementById("uploadedfile").value;

readFile(filePath);

}

 

 

Later, I need to read the file and I was trying to convert the Blob object to a String object to send the file using POST method

 

function readFile(filePath) {

if (blackberry.io.file.exists(filePath)){

blackberry.io.file.readFile(filePath,OnFileOpened,false);

}

}  

 

function OnFileOpened(filePath, blobData){

try {

stringFile = blackberry.utils.blobToString(blobData,"BASE64");

}catch(err){

var txt="There was an error on this page.\n\n";

txt+="Error description: " + err.description + "\n\n";

txt+="Click OK to continue.\n\n";

alert(txt);

}

alert("content size :"+stringFile.length );     

sendXML();

}

 

I send it using POST method

 

xmlHttp.open("POST",url,true);

var params = "ext=1111&uploadedfile="+stringFile;

xmlHttp.send(params);



 

 

I receive it in the upload.php

 

 

<?php

$uploadedfile =$_POST['uploadedfile'];

$contenido= base64_decode($uploadedfile);

 

$control = fopen($file,"w+"); 

$decoded=base64_decode($uploadedfile); 

if($control == false){  

die("No se ha podido crear el archivo.");  

}  

if (fwrite($control, $decoded) === FALSE) {       

echo "No se puede escribir al archivo ($file)";       

exit;   

}

fclose($control);

}

 

Sadly, it doesn't work.. I hope you understand what's going on here...  or something I'm missing.. Please.. 

 

Greetings, 

Emily

Please use plain text.