views:

93

answers:

1

I need to send a file via HTTP Post (a form) to more than one destination via a single click.

The form has a submit button

<input type="submit" id="button1" name="submit" value="upload file">

The action attribute and various different hidden fields that are needed, are set via JavaScript.

After button1 is clicked the file is sent to the first destination without problems.

It is impossible for me, to send the form a second time with different values because of the submit button.

I cannot use an external button like

<button id="button1">upload file</button>

This would make the problem easy to solve (I already tried it) but it is impossible here. The reason is that the message needs these lines at the end that are generated by a real submit button inside the HTML form:

Content-Disposition: form-data; name="submit"

upload file

My question: How can I send a form via JavaScript that includes a submit button? How can I "click" a submit button via JavaScript?

This is my Java Script file:

// If frame 1 is loaded, the function prepareUpload() will be executed
$("#frame1").ready(function() {  
   prepareUpload();  
}); 


function prepareUpload(){

     // click-handler is registered to button1
     $('#button1').click(function(){
            // if clicked...
            // "Please Wait" window
            $.blockUI({ css: { 
                border: 'none', 
                padding: '15px', 
                backgroundColor: '#000', 
                '-webkit-border-radius': '10px', 
                '-moz-border-radius': '10px', 
                opacity: .5, 
                color: '#fff' 
            } }); 

            //register load handler on iframe which triggers uploadData() 
            $('#frame1').load(function(){
                // reload from iframe
                uploadData();
            });

            iTargetNumber = 0;
            // call function uploadData()
            uploadData();
            return true;
        });
}


function uploadData() {
    var dataSet = data[iTargetNumber];
    document.getElementById("statusbar").style.width = (iTargetNumber / data.length * 200) + "px";

    // if file is uploaded to all servers...
    if (iTargetNumber == data.length) {
       document.getElementById("statustext").innerHTML = "done";
       // Reload browser window
       document.location.reload();
       return;
    }

    // status message
    document.getElementById("statustext").innerHTML = "uploading to " + dataSet.sUrl;

    //set "action" attribute in HTML form    
    $('#form1').attr('action', dataSet.sUrl);

    //set hidden fields in HTML form   
    for (var key in dataSet){
        $('#' + key).attr('value', dataSet[key]);
    }

    // HERE IT HANGS after the first run is completed because of the submit button.

    //submit HTML form with correct values
    $('#form1').submit();

    // increment variable +1
    iTargetNumber++;
}

// initialize variable 
var iTargetNumber = 0;

It hangs after the first form is send (because of the submit button).

Using

var myForm = document.forms.form1
var emptyForm = document.createElement('form');
emptyForm.submit.apply(myForm);

insted of

$('#form1').submit();

does not help too. The resulting message has no these lines included:

Content-Disposition: form-data; name="submit"

upload file

An additional hidden field like

<input type="hidden" id="submit" name="submit" value="submit">

that would create a Content-Disposition: form-data; name="submit" does not help too because it would be before the content of the file and there would be no upload file in the end.

================

The problem is solved. The solution is that the JavaScript funktion is called by a button

<button id="button1">upload file</button>

Inside the form is also a hidden submit button

<input type="submit" style="display:none" id="button2" name="submit" value="upload file">

The JavaScript funktion calls this hidden submit button. Now, it is working without problems.

A: 

Getting a submit method from another form should do the trick.

var myForm = document.forms.id_of_form;
var emptyForm = document.createElement('form');
emptyForm.submit.apply(myForm);
David Dorward
Where can I insert these lines?
Neverland
At the point where you want to submit the form.
David Dorward
I inserted your lines and erased $('#form1').submit(); But the message dont include Content-Disposition: form-data; name="submit" upload file. I checked it with wireshark and the server I neet to send the file to accepts it only when these lines are included
Neverland
Submit buttons are only successful when activated by the user. You will have to duplicate the data with a hidden input.
David Dorward
How can I duplicate the data with a hidden input? I need to send a form with a file selected to different servers and this can only happen with Java Script because a form can only have a single destination. Here is the full code: http://code.google.com/p/octopuscloud/
Neverland
You add an input to the form that has type hidden and the same name and value that you want.
David Dorward
But I need Content-Disposition: form-data; name="submit" and upload file at the end of the message send. This cannot be realized with a hiden field. Hidden fields are always before the file send.
Neverland
Isn't that a header set based on the form's enctype attribute?
David Dorward
enctype="multipart/form-data" is already in the header of the form.
Neverland