警报然后重新加载页面IN检测文件扩展名上传脚本 [英] alert then reload page IN detect file extension upload script

查看:109
本文介绍了警报然后重新加载页面IN检测文件扩展名上传脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数TestFileType(fileName,fileTypes ){
if(!fileName)return;

dots = fileName.split(。)
//获取最后一段时间后的部分。
fileType =。 + dots [dots.length-1];

return(fileTypes.join(。)。indexOf(fileType)!= -1)?
alert('正确的文件类型函数在这里'):
alert('错误的文件类型函数在这里');

with

 < input name =replay_fileid =replay_filetype =file/> 
< input type =submitid =upload_filevalue =Uploadname =uploadReplayonClick =TestFileType(this.form.replay_file.value,['w3g','。w3g'] ); />

我希望它提醒(错误的文件类型),然后重新加载页面(以便取消上传,而不是浪费时间),但到目前为止,我只能得到alrert框的工作,但不是页面重新加载功能后,页面重新加载将无法正常工作,我甚至尝试转到网址和窗口的位置,但它不会工作,将继续上传文件在警告框后:

  return(fileTypes.join(。)。indexOf(fileType) != -1)? 
null():
alert('Warcraft III replay file(.w3g)allowed only!'); window.location.reload();
}

我是否错过了某些东西,或者它会不会以这种方式工作来到文件上传?

解决方案使用 onsubmit 事件取消表单如果扩展名不正确,如下所示:

 函数TestFileType(fileName,fileTypes){
if(! fileName)return;

dots = fileName.split(。)
//获取最后一段时间后的部分。
fileType =。 + dots [dots.length-1]; (fileTypes.join(。)。indexOf(fileType)!= -1){
// alert('正确的文件类型函数在这里');


返回true;
} else {
alert('错误的文件类型函数在这里');
返回false;

}

onsubmit

 < form onsubmit =return TestFileType(this.form.replay_file .value,['w3g','。w3g']); ... 


I'm using this javascript to limit the extension of the file type when uploading:

function TestFileType( fileName, fileTypes ) {
if (!fileName) return;

dots = fileName.split(".")
//get the part AFTER the LAST period.
fileType = "." + dots[dots.length-1];

return (fileTypes.join(".").indexOf(fileType) != -1) ?
alert('Correct File Type Function Here') :
alert('Wrong File Type Function Here');
}

with

<input name="replay_file" id="replay_file" type="file"/> 
<input type="submit" id="upload_file" value="Upload" name="uploadReplay" onClick="TestFileType(this.form.replay_file.value, ['w3g','.w3g']);" />

I want it to alert (the wrong file type) and then reload the page (so that it cancels the upload instead of wasting time) but so far i only can get the alrert box to work but not the page reload function after that, the page reload won't work i even tried goto url and windows location but it won't work and will just continue uploading the file after the alert box:

return (fileTypes.join(".").indexOf(fileType) != -1) ?
null() :
alert('Warcraft III replay file (.w3g) allowed only!');window.location.reload();
}

Am i missing something or will it just won't work this way when it comes to file uploading?

解决方案

Use onsubmit event to cancel the form in case the extension is not correct, like this:

function TestFileType( fileName, fileTypes ) {
if (!fileName) return;

dots = fileName.split(".")
//get the part AFTER the LAST period.
fileType = "." + dots[dots.length-1];

if (fileTypes.join(".").indexOf(fileType) != -1) {
   //alert('Correct File Type Function Here');
   return true;
} else {
   alert('Wrong File Type Function Here');
   return false;
}
}

with the onsubmit event connected on your form element:

<form onsubmit="return TestFileType(this.form.replay_file.value, ['w3g','.w3g']);" ...

这篇关于警报然后重新加载页面IN检测文件扩展名上传脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆