Sunday, January 6, 2008

Submitting Files to Multiple Sites

An issue came up recently at my client where the client wanted to keep a backup copy of every file submitted by a user to a third-party site. The backup copy was going to be saved a separate server. The solution I implemented was some simple JavaScript code. Essentially, the code submits the form to one site then changes the "action" and "target" properties of the form and submits it again to the other site. The new target could be an HTML iframe or a new window. In my case it was a small popup window. Here is a sample of the code:


function submitForms() {
window.open("", "smallPopup", "location=1, status=1, scrollbars=1, width=100, height=100");
var theForm = document.myForm;
theForm.action = "http://firstSite.com/backup";
theForm.target = "smallPopup";
theForm.submit();

theForm.action = "http://2ndSite.com/receive";
theForm.target = "_self";
theForm.submit();
}

Now you might wonder why I didn't just create another form and have two forms, one for each server. The problem with that is the user would have to select the file twice, once for each form. This is because for security reasons, it is not possible to programmatically set the file path in an HTML input of type file. So a programmer is prevented from assigning the path of one input tag to be the same as another input tag of type file. A programmer only has read access to the file path. Of course this makes a lot of sense too because if it were possible then any malicious site would be able to upload any file its owner wishes from your computer to his server.

No comments: