cannot upload files and vars with xhr2 and web workers

213 views Asked by At

I try to create code to upload files using XHR2 and web workers. I thought I should use web workers , so if a file is big, web page will not freeze.

This is not working for two reasons, I never used web workers before, and I want to post to the server the file and vars at the same time, with the same xhr. When I say vars I mean the name of the file, and an int.

Heres is what I got

Client side

//create worker
var worker = new Worker('fileupload.js');
 worker.onmessage = function(e) {
 alert('worker says   '+e.data);
}

//handle workers error
worker.onerror =werror;
function werror(e) {
  console.log('ERROR: Line ', e.lineno, ' in ', e.filename, ': ', e.message);
 }

//send stuff to the worker
 worker.postMessage({
 'files' : files, //img or video
 'name' : nameofthepic, //text
 'id':imageinsertid //number
 });

Inside the worker (fileupload.js file)

onmessage = function (e) {var name=e.data.name; var id=e.data.id ; var file=e.data.files;

//create a var to catch the anser of the server
var datax;

      var xhr = new XMLHttpRequest();
               xhr.onload = function() {      
           if (xhr.status == 200) {datax=xhr.response;}
                   else { datax=525;}//actually, whatever, just give a value
        };
      xhr.open('POST', 'upload.php');                               

          xhr.send(file,name,id);
          //i also tried xhr.send('file=file&name=name&id=id');  and still nothing
          //i also tried  just the text/int xhr.send('name=name&id=id'); and still nothing

I am confused. I cannot send anything to the server. I get no feedback from the worker. I dont even know if the data are send to the fileupload.js. Server side does not INSERT.

Is that possible, sending files and text at the same time? What am I missing?

I need to pass text and int along with the file, so server side not only will upload the file, but also will INSERT to the database the int and the text, if the file is uploaded succesfully. This was easy just with formData and xhr, but, putting web workers in the middle, I cant get it right.

Also, can I use Transferable Objects to speed things up? Are Transferable Objects supported in all major browsers?

Thanks in advance

0

There are 0 answers