0

I have a web form that includes a file input (for a photo) and a text field (for a description).

When the user submits the form, I would like to POST the file to an API on another domain. The API call accepts binary file information. I was hoping to send the POST request via Ajax, but it seems it's not possible to send files via Ajax.

So, can I point the form directly at the API URL? The problem with this is what to do with the response: how can I handle a 200 response and forward the user to a 'success' page on my app, without Ajax?

Or do I need to do the POST in a server-side script instead?

simon
  • 5,987
  • 13
  • 31
  • 28

2 Answers2

0

I was hoping to send the POST request via Ajax, but it seems it's not possible to send files via Ajax.

Not only that you cannot upload files with AJAX but you cannot send AJAX request to different domains.

So here's what you could do:

Have the form POST to a server side script on your server. This script will fetch the uploaded file and description and it will send them as a new POST request to the distant domain. Based on the response your server side script will redirect to wherever you want.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

What about using a JavaScript library to help? You could use jQuery: How can I upload files asynchronously?

Community
  • 1
  • 1
Christopher Armstrong
  • 7,907
  • 2
  • 26
  • 28
  • I can use jQuery, yes. Maybe I should use the jQuery forms plugin? http://malsup.com/jquery/form/#getting-started – simon Apr 27 '11 at 17:45