Languages

Menu
Sites
Language
upload file to a webpage

Hello

so i'm trying to upload audio file located under media folder

the problem is the request is send but without the audio file

here the code

tizen.filesystem.resolve(
                'file:///opt/usr/media/test.aac'
                        ,
                function(dir){
                documentsDir = dir;
                console.log(dir.toURI( ));
                var formData = new FormData();
                formData.append("__VIEWSTATE","p57vGw5amg1TtebFeH1KccQSa93PaFJHyl7wFDHkGfW4jC01N8Fq9SiO21XtO6lAj5p5Nl5YQm3aE8Sq6u+v2gs9arrwbZhWMYuJ1s8VrP0=");
                formData.append("FileUploadControl", dir);
                formData.append("UploadButton", "Upload");
                client.open("post", "http://***********", true);
                client.send(formData);  
                dir.listFiles(onsuccesss, onerrors);
                }, function(e){
                console.log("Error" + e.message);
                }, "rw"
                );
client.onreadystatechange = function()
{  

   if (client.readyState == 4 && client.status == 200)
   {
       alert(client.responseText);
   }

Edited by: mohamed mlika on 08 May, 2015

Responses

10 Replies
AVSukhov

Hello,

To upload file to server you need get File or Blob object or byte array. In your example using FileStsrem API you are getting filePath - String, not file object.

For this purpose you need use W3C File API: input with type file, f.e.:

<input type="file" id="tizenFiles"/>

<script>
    var files = document.getElementById("tizenFiles").files[0];
</script>

or using FileSystem API read file as byte array.

And send it.

 

Marco Buettner

Your resolve ist wrong... tizen.filesystem.resolve() is only usable for the location

location can be a virtual root (images, documentation, videos etc.) or are path like "file://blabla/bla");

After you resolve the location, you can use resolve to fetch a file with file = dir.resolve(YOUR_FILENAME);

After that you can use file operation lie read file as byte array

mohamed mlika
hello again , so i managed to upload the file to the website but the audio file is not readable tizen.filesystem.resolve( 'file:///opt/usr/media/test.aac' , function(dir){ documentsDir = dir; if (dir != null) { dir.openStream( "rw", function(fs){ var bt= fs.readBytes(dir.fileSize); var blob = new Blob(bt,{type :'audio/mpeg'}); var formData = new FormData(); formData.append("__VIEWSTATE","jjj="); formData.append("FileUploadControl",blob,"mmm.aac"); formData.append("UploadButton", "Upload"); client.open("post", "/page", true); client.send(formData); fs.close(); }, function(e){ console.log("Error " + e.message); } ); } }, function(e){ console.log("Error" + e.message); }, "rw" );
mohamed mlika

please i need help

AVSukhov

Hello,

What do you mean "audio file is not readable"?

On server or client side?

mohamed mlika

on the server side 

i can't play the audio file 

 

AVSukhov

Hello,

Try to use:

var bt= fs.readBytes(dir.fileSize);
fs.close();
var byteArray = new Uint8Array(bt);
var blob = new Blob(byteArray,{type :'application/octet-stream'});

 

mohamed mlika

thank you for the help

but no

i think FormData don't support Uint8Array

 

AVSukhov

Hello,

Only for example try to use w3c File api with input type file:

html

<input type="file" id="fileToUpload" onchange="uploadAudio();" />

js

function uploadAudio() {
    var fileForUpload = document.getElementById('fileToUpload').files[0];
    var dataForUpload = new FormData();
    dataForUpload.append("your properties")
    dataForUpload.append("file_data", fileForUpload);
    var xhr = new XMLHttpRequest();
    xhr.addEventListener("load", function(evt) {
       ...
    }, false);
    xhr.open("POST", "...");
    xhr.send(dataForUpload);
}

 

mohamed mlika

hello 

thank you for helping me 

my project is like shazam so the user wil record a part of the song and then the tizen application will send it automatickliy send the song to the server 

so i can't use this example 

Kind Regards