Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using the ASP.NET FileUpload control.

To upload a file, a user must:

  1. Browse to select a file from his/her device,
  2. Click the upload button to upload the file to the server.

Is it possible to skip step 2 (the Upload button) and call the 'uploadFile_Click' function on the server through a jquery (postback) once a file is selected? I'm trying the following method but I need some help to finalize it.

ASPX PAGE

<asp:FileUpload runat="server" ID="UploadImages" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="UploadImages" ClientValidationFunction="ValidateUpload" />

CODE BEHIND

protected void uploadFile_Click(object sender, EventArgs e)
{
// Upload code
}

JQUERY

function ValidateUpload() {
var FileUpload_function = document.getElementById('UploadImages');

        if (FileUpload_function.value == '') {
            return false;
        }
        else {

           ** Here I want to trigger a postback to call the uploadFile_Click. Is that possible? **
        }
        return true;

    }
share|improve this question
    
So my approach has a dead end...? –  Gloria Nov 13 at 21:12
    
Usually when you want to do anything more than just the basic select a file and click upload, you gotta roll your own solution. There are some premade solutions that you can try, most of which rely on ajax calls to process the upload and have features like visual upload progress, drag and drop, etc. –  ElGavilan Nov 13 at 21:18

1 Answer 1

Some (most?) browsers will not allow you to call the click event on an input HTML element if it is of type file (ie. an asp:fileupload control). It is considered a security risk.

share|improve this answer
    
Even if it does work it won't include the file data when posting the form. Same reason. –  Christopher White Nov 13 at 21:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.