日期:2014-05-18  浏览次数:20856 次

文件上传下载问题
最近做文件上传下载时遇到2个问题,请求各位帮帮忙。
1.文件上传时,文件重名时没有提示就直接覆盖了,怎么做能让他有个提示?
2.文件下载时,如果文件名中带符号,如“+”“%”之类,就不能下载。

------解决方案--------------------
最近我所做的项目也用到了类似的文件的上传下载部分功能,代码基本上和你所说的很类似,下面我将部分代码提供给你,仅供参考:(主要是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 "/> &nbsp;&nbsp;
<html:button value= "上传 " property= " " onclick= "uploadAttachment(); " /> &nbsp;
</td>
</tr>
</table>