文件上传下载问题 最近做文件上传下载时遇到2个问题,请求各位帮帮忙。1.文件上传时,文件重名时没有提示就直接覆盖了,怎么做能让他有个提示?2.文件下载时,如果文件名中带符号,如“+”“%”之类,就不能下载。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 最近我所做的项目也用到了类似的文件的上传下载部分功能,代码基本上和你所说的很类似,下面我将部分代码提供给你,仅供参考:(主要是js代码)(采用的技术主要是struts技术)function uploadAttachment(){ var surFileName = document.forms[0].theFile.value; if(surFileName == null || surFileName == "") { alert("请先选择要上传的文件"); return; } var filePostfix = getFilePostfix(document.forms[0].theFile); var filename = document.forms[0].fileName.value + "." + filePostfix; var fileid = "att"+document.forms[0].attachmentID.value; for(var i=0;i<document.forms[0].fileList.length;i++){ if(document.forms[0].fileList.options[i].text == filename){ alert("附件重名!!!"); return; } } var newAttachment=document.createElement("option"); newAttachment.text = filename; newAttachment.value = fileid; document.forms[0].fileList.add(newAttachment, document.forms[0].fileList.length); try{ opener.document.forms[0].attachmentlist.value += filename+"; "; }catch(e){ alert(e.name); alert(e.message); } document.forms[0].action.value="attachment_Upload"; document.uploadActionForm.submit();}/***得到文件的后缀名*oFile为file控件对象*/function getFilePostfix(oFile){ if(oFile == null) return null; var pattern = /(.*)/.(.*)$/gi; if(typeof(oFile) == "object") { if(oFile.value == null || oFile.value == "") return null; var arr = pattern.exec(oFile.value); return RegExp.$2; } else if(typeof(oFile) == "string") { var arr = pattern.exec(oFile); return RegExp.$2; } else return null;}/***得到文件的后缀名*oFile为file控件对象*/function getFilePostfix(oFile){ if(oFile == null) return null; var pattern = /(.*)/.(.*)$/gi; if(typeof(oFile) == "object") { if(oFile.value == null || oFile.value == "") return null; var arr = pattern.exec(oFile.value); return RegExp.$2; } else if(typeof(oFile) == "string") { var arr = pattern.exec(oFile); return RegExp.$2; } else return null;}<td align="center" valign="top"> <table width="100%" border="0" align="center" cellpadding="3" cellspacing="1" class="table2"> <tr class="tr1"> <td width="13%" align="center">文件名称</td> <td width="87%"> <html:text property="fileName"> </html:text> </td></tr> <tr class="tr1"> <td width="13%" align="center">上传</td> <td width="87%" align="left"> <html:file size="45" property="theFile"/> <html:button value="上传" property="" onclick="uploadAttachment();" /> </td> </tr> </table> </td>此代码仅供参考,如有疑问,请及时发布。 jsp 如何获取当前页面之前的页面名称?????????????? $.POST报错 贡献所有的分:求在jsp页面中将从数据库中查询出来的集合导入到Excel表中 ajax校验checkbox 向mysql中插入中文问题 过滤器以及下载工具下载的问题~ 潇湘上传组件 做项目前端用PHP底层用java怎么实现? jsp页面中,如何让某个语句只执行一次? 有什么办法可以把class文件反编译回java文件? 关于Ajax在火狐下不执行的问题? 我想在tomcat5.5配置两个项目,分别用两个端口访问,请问该怎么设置呢?
var surFileName = document.forms[0].theFile.value;
if(surFileName == null || surFileName == "") {
alert("请先选择要上传的文件");
return;
}
var filePostfix = getFilePostfix(document.forms[0].theFile);
var filename = document.forms[0].fileName.value + "." + filePostfix;
var fileid = "att"+document.forms[0].attachmentID.value;
for(var i=0;i<document.forms[0].fileList.length;i++){
if(document.forms[0].fileList.options[i].text == filename){
alert("附件重名!!!");
return;
}
}
var newAttachment=document.createElement("option");
newAttachment.text = filename;
newAttachment.value = fileid;
document.forms[0].fileList.add(newAttachment, document.forms[0].fileList.length);
try{
opener.document.forms[0].attachmentlist.value += filename+"; ";
}catch(e){
alert(e.name);
alert(e.message);
}
document.forms[0].action.value="attachment_Upload";
document.uploadActionForm.submit();
}/**
*得到文件的后缀名
*oFile为file控件对象
*/
function getFilePostfix(oFile)
{
if(oFile == null)
return null;
var pattern = /(.*)/.(.*)$/gi;
if(typeof(oFile) == "object")
{
if(oFile.value == null || oFile.value == "")
return null;
var arr = pattern.exec(oFile.value);
return RegExp.$2;
}
else if(typeof(oFile) == "string")
{
var arr = pattern.exec(oFile);
return RegExp.$2;
}
else
return null;
}/**
*得到文件的后缀名
*oFile为file控件对象
*/
function getFilePostfix(oFile)
{
if(oFile == null)
return null;
var pattern = /(.*)/.(.*)$/gi;
if(typeof(oFile) == "object")
{
if(oFile.value == null || oFile.value == "")
return null;
var arr = pattern.exec(oFile.value);
return RegExp.$2;
}
else if(typeof(oFile) == "string")
{
var arr = pattern.exec(oFile);
return RegExp.$2;
}
else
return null;
}<td align="center" valign="top">
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1" class="table2">
<tr class="tr1">
<td width="13%" align="center">文件名称</td>
<td width="87%">
<html:text property="fileName">
</html:text>
</td></tr>
<tr class="tr1">
<td width="13%" align="center">上传</td>
<td width="87%" align="left">
<html:file size="45" property="theFile"/>
<html:button value="上传" property="" onclick="uploadAttachment();" />
</td>
</tr>
</table>
</td>
此代码仅供参考,如有疑问,请及时发布。