Vinc 웃
Vinc 웃

Reputation: 1247

Javascript Validation FileUpload and server upload

I have a little problem with a page im trying to create. In resume, I want to validate if a file has been selected by the user and then upload it on the server. I know it is possible to do it throught javascript :

if(document.getElementById("uploadBox").value != "") {
   // you have a file
}

But the thing is if I add the property runat="server" to my input :

<input id="myFile" type="file" runat="server" />

I can't validate anymore. So, do you know a way to do what I am trying to do.

PS: Keep in mind that I want to validate with javascript to catch the postback and then show an error message using ajax. That's why im not doing the validation trough server code.

Thank you :)

Upvotes: 0

Views: 142

Answers (1)

nmat
nmat

Reputation: 7591

That happens because the ID of the input control is changed when you run it server side. Here are two ways to solve it, either force the ID to stay the same with this:

<input id="myFile" type="file" runat="server" ClientIDMode="Static" />

or, if you have the Javascript in the same ASP page, you can write the ID directly like this:

if(document.getElementById('<%= uploadBox.ClientID %>').value != "") {
   // you have a file
}

Upvotes: 1

Related Questions