使用 Asp.Net 控件启动 jQuery 方法
Launching jQuery method using Asp.Net control
当我在 Asp.Net 中 select 来自 FileUpload 控件的文件时,我尝试启动 jQuery 方法,但它不起作用。 jQuery 方法必须检查 selected 的文件大小。我该如何解决这个问题?
<script type="text/javascript">
function ValidateUploadButton() {
//this code will be executed when a new file is selected
$('#FileUpload1').bind('change', function () {
//converts the file size from bytes to MB
var fileSize = this.files[0].size / 1024;
//checks whether the file is less than 1 MB
if (fileSize > 1) {
$('#errorMessage').html("The file is too big")
alert("okey");
//successfully validated
}
else {
alert("else");
}
});
}
</script>
<asp:FileUpload ID="FileUpload1" runat="server" ClientValidationFunction="ValidateUploadButton" />
<span id="errorMessage"></span>
我将您的 ClientValidationFunction 属性更改为 onchange。
<asp:FileUpload ID="FileUpload1" runat="server" onchange="ValidateUploadButton();" />
<span id="errorMessage"></span>
编辑脚本以删除 'change' 绑定,因为现在由于更改而调用 ValidateUploadButton。
<script type="text/javascript">
function ValidateUploadButton()
{
//this code will be executed when a new file is selected
//converts the file size from bytes to Ko
var fileSize = $('#FileUpload1')[0].files[0].size / 1024 / 1024;
// USE THIS BELOW IF THE ONE ABOVE DOESN'T WORK
//var fileSize = $('#<%= FileUpload1.ClientID %>')[0].files[0].size / 1024 / 1024;
//checks whether the file is less than 1 MB
if (fileSize > 1)
{
$('#errorMessage').html("The file is too big")
alert("okey");
//successfully validated
}
else
{
alert("else");
}
}
</script>
编辑
抱歉,我没能彻底测试它。它现在应该工作了。正确引用文件大小的 javascript 语法是错误的。我还添加了 1024 的额外除法,因此您将以 MB 而不是 KB 为单位获得它。
注意 - 这仅适用于一个文件,不支持多个文件。
当我在 Asp.Net 中 select 来自 FileUpload 控件的文件时,我尝试启动 jQuery 方法,但它不起作用。 jQuery 方法必须检查 selected 的文件大小。我该如何解决这个问题?
<script type="text/javascript">
function ValidateUploadButton() {
//this code will be executed when a new file is selected
$('#FileUpload1').bind('change', function () {
//converts the file size from bytes to MB
var fileSize = this.files[0].size / 1024;
//checks whether the file is less than 1 MB
if (fileSize > 1) {
$('#errorMessage').html("The file is too big")
alert("okey");
//successfully validated
}
else {
alert("else");
}
});
}
</script>
<asp:FileUpload ID="FileUpload1" runat="server" ClientValidationFunction="ValidateUploadButton" />
<span id="errorMessage"></span>
我将您的 ClientValidationFunction 属性更改为 onchange。
<asp:FileUpload ID="FileUpload1" runat="server" onchange="ValidateUploadButton();" />
<span id="errorMessage"></span>
编辑脚本以删除 'change' 绑定,因为现在由于更改而调用 ValidateUploadButton。
<script type="text/javascript">
function ValidateUploadButton()
{
//this code will be executed when a new file is selected
//converts the file size from bytes to Ko
var fileSize = $('#FileUpload1')[0].files[0].size / 1024 / 1024;
// USE THIS BELOW IF THE ONE ABOVE DOESN'T WORK
//var fileSize = $('#<%= FileUpload1.ClientID %>')[0].files[0].size / 1024 / 1024;
//checks whether the file is less than 1 MB
if (fileSize > 1)
{
$('#errorMessage').html("The file is too big")
alert("okey");
//successfully validated
}
else
{
alert("else");
}
}
</script>
编辑
抱歉,我没能彻底测试它。它现在应该工作了。正确引用文件大小的 javascript 语法是错误的。我还添加了 1024 的额外除法,因此您将以 MB 而不是 KB 为单位获得它。
注意 - 这仅适用于一个文件,不支持多个文件。