<asp:FileUpload ID="FileUpload1" runat="server" Width="100%" onchange="return checkFileExtension(this);" />
<script type="text/javascript">
function checkFileExtension(elem) {
var filePath = elem.value;
if (filePath.indexOf('.') == -1) {
remove(elem);
alert('不正確的副檔名格式');
return false;
}
var validExtensions = new Array();
var ext = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();
validExtensions[0] = 'tif';
validExtensions[1] = 'tiff';
validExtensions[2] = 'pdf';
for (var i = 0; i < validExtensions.length; i++) {
if (ext == validExtensions[i])
return true;
}
remove(elem);
alert('不正確的副檔名格式: ' + ext.toUpperCase());
return false;
}
function remove(control) {
var who = control;
who.value = "";
var who2 = who.cloneNode(false);
who2.onchange = who.onchange;
who.parentNode.replaceChild(who2, who);
}
</script>