0

Every so often (seems about once a year) I get a chance to play with JS.
While not proficient, I'm comfortable enough to get the job done if I have some good references.

At this point, I feel like I'm just not coming up with the right search terms to find anything useful.

GOAL:
I'm looking for examples, tips, advice and/or links to any documentation/articles/instructions that my help.

BACK STORY:
We have a vendor on whose site we search part numbers.
The results page has basic spec's and stat's, and there is a download link for a pricing sheet.
We have developed a tool to parse through the sheet and automatically enter the pricing into our system. The current workflow is to download the file and save it locally, and then upload the file to our form.

TASK:
What I intend to do is make a bookmarklet to cut out a few steps.
I'd like to have the JS initiate the DL and then POST the file right off to the current form-processing action url (straight from memory/temp location).

SUMMARY:
Of course, snippets and examples are always awesome!!
Beyond that, any-and-every bit of sharing will be appreciated

mOrloff
  • 2,547
  • 4
  • 29
  • 48
  • Will you enounter the _same origin policy_ or are the places you're uploading to and downloading from the same origin/[CORS](https://developer.mozilla.org/en/docs/HTTP/Access_control_CORS) enabled? – Paul S. Sep 12 '13 at 15:55
  • Alas, we will not pass the _same origin policy_ :( – mOrloff Sep 12 '13 at 17:17

1 Answers1

0

Assuming you're passing the same origin policy and that your browser supports XMLHttpRequest, Blob, FormData

function getThenUpload(fromURL, toURL) {
    var xhrFrom = new XMLHttpRequest();
    xhrFrom.onload = function () {
        var xhrTo = new XMLHttpRequest(),
            formData = new FormData();    // make file have <form> style
        formData.append('file', this.response); // set "name, value" of "<input>"
        xhrTo.open('POST', toURL);
        xhrTo.send(formData);             // upload file
    }
    xhrFrom.open('GET', fromURL);
    xhrFrom.responseType = 'blob';        // expecting file
    xhrFrom.send();                       // download file
}
getThenUpload('/file.txt', 'upload.php'); // start
Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • Thanks for the fantastic info! While I think the Same Origin Policy will be a hangup, this info & code is quite helpful. – mOrloff Sep 12 '13 at 17:18
  • @mOrloff If you are only able to enable CORS on just the one side try this code from both sides and perhaps one will work – Paul S. Sep 12 '13 at 17:22