简体   繁体   English

Javascript验证FileUpload和服务器上传

[英]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 : 我知道可以通过javascript来做到这一点:

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

But the thing is if I add the property runat="server" to my input : 但是事情是,如果我将属性runat =“ server”添加到我的输入中:

<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. PS:请记住,我想使用javascript进行验证以捕获回发,然后使用ajax显示错误消息。 That's why im not doing the validation trough server code. 这就是为什么我不执行验证槽服务器代码的原因。

Thank you :) 谢谢 :)

That happens because the ID of the input control is changed when you run it server side. 发生这种情况是因为在服务器端运行输入控件时ID发生了变化。 Here are two ways to solve it, either force the ID to stay the same with this: 解决此问题的方法有两种,一种是强制ID与此保持不变:

<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: 或者,如果您在同一ASP页面中包含Javascript,则可以直接这样编写ID:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM