views:

46

answers:

1

I'm trying to create a simple "AJAX" uploader (as i know you can't actually do an AJAX upload). I'm having issues and i REALLY don't want to use a plugin. I'm writing an app that needs to have PDF attachments as part of a form, and my app's code is already at about 1000 lines of JS + jQuery. I don't want to add on an extra 1000k of SWFObject or a giant form plugin. Plus, i like learning things so I know how they work :) and i'm fairly proficient in JS as well...

Here is what I have so far:

<!DOCTYPE html>
<html>
    <head>
        <title>Upload Test</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
        <script>            
            $(function(){
                $('#file_upload').submit(function(){
                    $('#file_upload').attr('target','upload_target');
                    alert($('iframe').contents().text());
                    return false;

                });
            });
        </script>
    </head>
    <body>
        Upload Test to 382
        <form id="file_upload" method="post" enctype="multipart/form-data" action="io.cfm?action=updateitemfile&item_id=382">
            <input name="binary" id="file" size="27" type="file" /><br />
            <input type="submit" name="action" value="Upload" /><br />
            <iframe id="upload_target" name="upload_target" src="" style=""></iframe>
        </form>

    </body>
</html>

I keep reading that if i add the target attr to a form the form will submit there, but no luck at all. It just doesn't put anything in the iframe. The "action" attr will be dynamic based on what item is selected. If i turn off all the "AJAX" it works fine too, and i don't have access to the backend as it's an internal API.

So, how do I submit this to the iframe and then get the iframe's content which will be a JSON response?

P.S. this is just a test page im working in... obv this isn't the entire app.

+1  A: 

It's not that the submit of the form populates the <iframe>. The POST should make it back to the server - are you seeing that? Then, the server response will end up in the <iframe>. Typically what you do in order to make it feel sort-of like Ajax is to have the response page contain some Javascript that talks back to the main page somehow.

Stop returning false from your submit handler and see what happens.

The point of doing this is to avoid the default behavior of form submission. When you submit a form in the plain old HTML way, the page containing the form is replaced by the response from the server. When you target the form at an <iframe>, however, it's not. Instead the response is directed at the "page" inside the frame.

Pointy
I'm getting prompted to download the io.cfm file?
Oscar Godson
Is it because it's returning raw JSON? I can request this to be changed from the server guys, but that could take some time and i'd like to get a JSON response if possible.
Oscar Godson
A JSON response will do you no good at all, because the browser is not going to know what to do with it. Check the headers in the response; if it's offering a file for download, then there's even less chance of making it work.
Pointy
what format should I ask them to give it to me in? What about XML or HTML or a text header which returns the JSON and then my JS unstringifys it?
Oscar Godson
Wait - where is this "io.cfm" URL? Is that a real URL or is it just a file? It's going to be somewhere between "real hard" and "impossible" to test this without some sort of server running, even if it's just CGI.
Pointy
The best format would be an HTML page with a `<script>` tag in it that you control. There doesn't have to be any visible content in the page at all. It's just there to tell the code on the main page that the upload action completed, and possibly to give you error feedback if something went wrong.
Pointy
No, it's running on a server. I just work for a government, and im a front end guy i dont have access to the server files is all. I just have to tell them what to do ;) but, no, it's on a server and it's parsing my uploads correctly and giving me the right JSON return, when i do it w/o AJAX, but WITH AJAX and putting it into the iframe it prompts me to download it. I figured it was because it was a real JSON file and im trying to display it. I just want a response with a yes/no and if yes, the file ID. thats all.
Oscar Godson
OK, so, if I do the HTML w/ a script tag, how do I get the code thats in that script to run on the parent page, not the .io.cfm?
Oscar Godson
Well that part's just weird. See what happens if you change the form so that it's got a "target" attribute right on it, in the original markup. Then you can take out that jQuery code entirely (at least for the time being). Just say `target='upload_target'` in the `<form>` tag.
Pointy
You also might look at what seems different about the request and response headers - the Firefox plugin "TamperData" is my favorite way of doing that.
Pointy
No, it's not weird after I thought about it. If I use the other API calls that don't send binary data i get a JSON response and im prompted to download those also. I'll try out tampadata also. Thanks man! You've helped a lot! Thanks for explaining that too.
Oscar Godson
I got it to work and it was super easy. I just changed it to a text type :) thanks!
Oscar Godson
Great! best of luck.
Pointy