怎么用js来获取 fileupload中的上传文件的文件名。
2个回答
展开全部
在firefox上,input(type=file)默认获取到的value值就是文件名。
在IE上,input(type=file)默认获取到的value值是路径。
因此用js来获取fileupload中的上传文件的文件名需要多方面考虑。
你可以试下一下
function getFileName(path){
var pos1 = path.lastIndexOf('/');
var pos2 = path.lastIndexOf('\\');
var pos = Math.max(pos1, pos2)
return path;elsereturn path.substring(pos+1);}测试过了,没有问题,可用。
展开全部
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>批量上传</title>
<script type="text/javascript">
var attachname = "attach";
var i=1;
function addInput(){
if(i<10){
var attach = attachname + i ;
if(createInput(attach))
i=i+1;
}
}
function deleteInput(){
if(i>1){
i=i-1;
if(!removeInput())
i=i+1;
}
}
function createInput(nm){
var aElement=document.createElement("div");
var html = "附件("+i+")<input type='file' id='FileUpload"
+ i + "' name='FileUpload"
+ i + "' title='选择图片' size='45' onchange='javascript:showphoto();'><br />"
+ "<img alt='js 用户自定义多文件上传代码' width='0' id='Image"
+ i + "' name='Image"
+ i + "' src='' title='图片预览' style='margin-left: 55px;' >";
aElement.innerHTML=html;
//aElement.value="thanks";
//aElement.onclick=Function("asdf()");
if(document.getElementById("upload").appendChild(aElement) == null)
return false;
return true;
}
function removeInput(nm){
var aElement = document.getElementById("upload");
if(aElement.removeChild(aElement.lastChild) == null)
return false;
return true;
}
function showphoto(){
var re=/.(jpg|jpeg|JPG|JPEG)$/;
var x=document.getElementById('upload').getElementsByTagName('input');
for(r=0;r<x.length;r++){
if ((!re.test(document.getElementById("FileUpload"+[r+1]).value)) & (document.getElementById("FileUpload"+[r+1]).value!=""))
{
alert("只允许上传jpg文件!");
document.getElementById("FileUpload"+[r+1]).outerHTML=document.getElementById("FileUpload"+[r+1]).outerHTML.replace(/value=w/g,'');
document.getElementById("Image"+[r+1]).style.width="0px";
document.getElementById("Image"+[r+1]).src="";
}else if (document.getElementById("FileUpload"+[r+1]).value!="")
{
document.getElementById("Image"+[r+1]).style.width="100px";
document.getElementById("Image"+[r+1]).src=document.getElementById("FileUpload"+[r+1]).value;
}
}
}
function newInput(){
var x=document.getElementById('upload').getElementsByTagName('img');
for(r=0;r<9;r++){
addInput();
x[r].width="100";
x[r].src=document.getElementById("smPicture").value.replace("sm","sm"+[r+1]);
//x[r].onerror=function(){deleteInput()};
}
}
</script>
<!--
<script type="text/javascript">
onload=function(){
newInput();
}
</script>
-->
</head>
<body>
<input type="button" name="button" value="添加附件" onclick="addInput()">
<input type="button" name="button" value="删除附件" onclick="deleteInput()">
<span id="upload"></span>
<input name="smPicture" type="hidden" id="smPicture" value='http://222.70.206.26/img/5/5000011_sm.jpg'>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>批量上传</title>
<script type="text/javascript">
var attachname = "attach";
var i=1;
function addInput(){
if(i<10){
var attach = attachname + i ;
if(createInput(attach))
i=i+1;
}
}
function deleteInput(){
if(i>1){
i=i-1;
if(!removeInput())
i=i+1;
}
}
function createInput(nm){
var aElement=document.createElement("div");
var html = "附件("+i+")<input type='file' id='FileUpload"
+ i + "' name='FileUpload"
+ i + "' title='选择图片' size='45' onchange='javascript:showphoto();'><br />"
+ "<img alt='js 用户自定义多文件上传代码' width='0' id='Image"
+ i + "' name='Image"
+ i + "' src='' title='图片预览' style='margin-left: 55px;' >";
aElement.innerHTML=html;
//aElement.value="thanks";
//aElement.onclick=Function("asdf()");
if(document.getElementById("upload").appendChild(aElement) == null)
return false;
return true;
}
function removeInput(nm){
var aElement = document.getElementById("upload");
if(aElement.removeChild(aElement.lastChild) == null)
return false;
return true;
}
function showphoto(){
var re=/.(jpg|jpeg|JPG|JPEG)$/;
var x=document.getElementById('upload').getElementsByTagName('input');
for(r=0;r<x.length;r++){
if ((!re.test(document.getElementById("FileUpload"+[r+1]).value)) & (document.getElementById("FileUpload"+[r+1]).value!=""))
{
alert("只允许上传jpg文件!");
document.getElementById("FileUpload"+[r+1]).outerHTML=document.getElementById("FileUpload"+[r+1]).outerHTML.replace(/value=w/g,'');
document.getElementById("Image"+[r+1]).style.width="0px";
document.getElementById("Image"+[r+1]).src="";
}else if (document.getElementById("FileUpload"+[r+1]).value!="")
{
document.getElementById("Image"+[r+1]).style.width="100px";
document.getElementById("Image"+[r+1]).src=document.getElementById("FileUpload"+[r+1]).value;
}
}
}
function newInput(){
var x=document.getElementById('upload').getElementsByTagName('img');
for(r=0;r<9;r++){
addInput();
x[r].width="100";
x[r].src=document.getElementById("smPicture").value.replace("sm","sm"+[r+1]);
//x[r].onerror=function(){deleteInput()};
}
}
</script>
<!--
<script type="text/javascript">
onload=function(){
newInput();
}
</script>
-->
</head>
<body>
<input type="button" name="button" value="添加附件" onclick="addInput()">
<input type="button" name="button" value="删除附件" onclick="deleteInput()">
<span id="upload"></span>
<input name="smPicture" type="hidden" id="smPicture" value='http://222.70.206.26/img/5/5000011_sm.jpg'>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询