views:

1078

answers:

3

Hello,

I am trying to have a single text link on which the user clicks and it asks the user which file he/she wants to upload then, auto matically POSTs it to the form. How can I achieve the same? I know I have to style my file input but how to get it to post automatically on file selection?

Thank you very much

A: 

You can call the submit method of your form. It will submit your page to the server and you can get the file in the Files collection of the request.

You'll have to call the submit() method of the form on the "onchange" event of the "file" element.

<input type="file" onchange="formname.submit();" id="f" />

EDIT: Try this.

Kirtan
yes i managed that. but how do i convert the whole input thing to text - "upload file"
Alec Smart
A: 

Embedding javascript in the page is bad practice.

Add the submit to the item using a selector. This also enables you to add the functionality to a basic link, as you have requested.

HTML

<form id="myForm">
    <a id="uploadLink" href="#">Upload file</a>
</form>

jQuery

$(document).ready(function() {
    $("#uploadLink").click(function() {
     $("#myForm").submit();
    }
});

Additonal resources

Here is a link to the jQuery Form plugin.

There is also a multi-file upload plugin for jQuery.

Jon Winstanley
A: 

There is two mistakes

Gregory