views:

1496

answers:

6

I am trying to upload a file using to Flickr using JQuery. I have a form (which works if I dont use JQuery) which I am submitting using the Form Plugin. My code is as follows:

<html>

<head>
<title>Test Upload</title>
<script type="text/javascript" src="jquery-1.2.6.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $('#myForm').bind('submit', function() {
        $(this).ajaxSubmit({
            dataType: 'xml',
            success:  processXml
        });
        return false; // <-- important!
    });
});

function processXml(responseXML) {
    var message = $('message', responseXML).text();
    document.getElementById('output').innerHTML = message;
}

</script>

</head>

<body>

<form id="myForm" method="post" action="http://api.flickr.com/services/upload/" enctype="multipart/form-data">
<input type="file" name="photo" id="photo"/>
<input type="text" name="api_key" id="api_key" value="..snip.."/>
<input type="text" name="auth_token" id="auth_token" value="..snip.."/>
<input type="text" name="api_sig" id="api_sig" value="..snip.."/>
<input type="submit" value="Upload"/>
</form>
<div id="output">AJAX response will replace this content.</div>
</body>

</html>

The problem is I get the following text as a response:

<rsp stat="fail">

    <err code="100" msg="Invalid API Key (Key not found)" />

</rsp>

even though the file uploads with no problems. This means my div is not updated as it doesnt run the success function. Any one have any ideas.

Thanks

A: 

You will not be able to upload a file via AJAX this way.

A pure AJAX file upload system is not possible because of security limitations of JavaScript.

Devon
A: 

Devon:: The file does upload but I can't seem to get to the returned XML.

Are you sure that the API key you are submitting is valid?
Devon
He also said that code works when he's not using jQuery
Mark Biek
A: 

I see that you're using ajaxSubmit. That's the jQuery Form Plugin, right? Is it possible that the issue is something with that?

Have you tried using jQuery.post instead?

Mark Biek
+1  A: 

See this other thread about uploading files with AJAX:

http://stackoverflow.com/questions/166221/how-to-upload-file-jquery

I've never tried it, but it seems that you can't get the server response (not easily, anyway)

Brock Boland
A: 

ajax does not work cross-domain. You can not submit a form using ajax from one domain to another domain.

adnan
A: 

what you can do is - use a proxy.php file on your domain. submit the form using ajax to proxy.php. The code in your proxy.php will submit the form using CURL to flickr. You'll get the CURL code on php.net or many other sites

adnan