从网上找的js代码,结合VS2005又改动了一下,可以重命名文件,可以在上传的多个文件中删掉选中的~~~
aspx:
<%
@ Page language="c#" Inherits="Attachment" CodeFile="Attachment.aspx.cs"
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
<
HTML
>
<
HEAD
>
<
title
>
Attachment
</
title
>
<
META
http-equiv
="Content-Type"
content
="text/html; charset=gb2312"
>
<
meta
name
="GENERATOR"
Content
="Microsoft Visual Studio .NET 7.1"
>
<
meta
name
="CODE_LANGUAGE"
Content
="C#"
>
<
meta
name
="vs_defaultClientScript"
content
="JavaScript"
>
<
meta
name
="vs_targetSchema"
content
="http://schemas.microsoft.com/intellisense/ie5"
>
<
LINK
href
="../css/cold.css"
type
="text/css"
rel
="stylesheet"
>
<
asp:Literal
id
="litCSS"
runat
="server"
></
asp:Literal
>
</
HEAD
>
<
body
>
<
form
id
="Form1"
method
="post"
enctype
="multipart/form-data"
runat
="server"
>
<
FONT
face
="宋体"
>
<
table
width
="100%"
height
="100%"
border
="0"
cellpadding
="0"
cellspacing
="0"
>
<
tr
>
<
td
height
="10"
><
img
src
="../images/main_top_left.gif"
width
="11"
height
="10"
></
td
>
<
td
background
="../images/main_top_image.gif"
></
td
>
<
td
><
img
src
="../images/main_top_right.gif"
width
="11"
height
="10"
></
td
>
</
tr
>
<
tr
>
<
td
width
="11"
background
="../images/main_left_image.gif"
></
td
>
<
td
align
="center"
vAlign
="top"
bgColor
="#ffffff"
>
<
br
>
<
TABLE
cellSpacing
="0"
cellPadding
="1"
width
="300"
border
="1"
>
<
TR
>
<
TD
colspan
="2"
class
="left_button2"
>
添加附件
</
TD
>
</
TR
>
<
TR
>
<
TD
colspan
="2"
>
1.选择要添加的附件
</
TD
>
</
TR
>
<
tr
>
<
TD
colSpan
="2"
><
INPUT
id
="FileUpload"
style
="WIDTH: 280px; HEIGHT: 23px"
type
="file"
onchange
="setFlag();"
size
="27"
name
="FileUpload"
runat
="server"
><
br
>
<
input
id
="chkUpLoadFolder"
onclick
="setWritable();"
type
="checkbox"
value
="on"
name
="chkUpLoadFolder"
runat
="server"
>
上传该文件所在文件夹下的所有文件
<
br
>
<
div
id
="div_message"
></
div
>
</
TD
>
</
tr
>
<
TR
>
<
TD
colspan
="2"
>
2.输入新的名称
</
TD
>
</
TR
>
<
TR
>
<
TD
colSpan
="2"
><
INPUT
id
="TxtFileName"
style
="WIDTH: 200px; HEIGHT: 23px"
type
="text"
size
="28"
name
="TxtFileName"
runat
="server"
>
<
INPUT
id
="CmdUpLoad"
style
="WIDTH: 72px; HEIGHT: 25px"
onclick
="upload();"
type
="button"
value
="上传文件"
name
="CmdUpLoad"
>
<
input
id
="hidMaxFileSize"
type
="hidden"
name
="hidMaxFileSize"
runat
="server"
>
<
input
id
="hidMaxUpLoadSize"
type
="hidden"
name
="hidMaxUpLoadSize"
runat
="server"
>
<
input
id
="hidPrjID"
type
="hidden"
name
="hidPrjID"
runat
="server"
>
<
input
id
="hidFileList"
type
="hidden"
Runat
="server"
name
="hidFileList"
>
</
TD
>
</
TR
>
<
TR
>
<
TD
colSpan
="2"
>
3.已添加的附件
</
TD
>
</
TR
>
<
TR
>
<
TD
vAlign
="middle"
><
SELECT
id
="LstFileList"
style
="WIDTH: 200px"
size
="6"
name
="LstFileList"
runat
="server"
></
SELECT
>
</
TD
>
<
td
vAlign
="middle"
align
="center"
><
INPUT
class
="button"
id
="CmdDelFile"
type
="button"
value
="删除"
name
="CmdDelFile"
onclick
="remove();"
>
</
td
>
</
TR
>
<
TR
>
<
TD
colspan
="2"
align
="center"
><
INPUT
type
="button"
value
="确定"
class
="button"
id
="CmdOK"
runat
="server"
NAME
="CmdOK"
onserverclick
="CmdOK_ServerClick"
>
<
INPUT
class
="button"
type
="button"
value
="取消"
id
="CmdCancel"
runat
="server"
NAME
="CmdCancel"
onserverclick
="CmdCancel_ServerClick"
>
</
TD
>
</
TR
>
</
TABLE
>
</
td
>
<
td
width
="11"
background
="../images/main_right_image.gif"
></
td
>
</
tr
>
<
tr
>
<
td
height
="11"
><
img
src
="../images/main_down_left.gif"
width
="11"
height
="11"
></
td
>
<
td
background
="../images/main_down_image.gif"
></
td
>
<
td
><
img
src
="../images/main_down_right.gif"
width
="11"
height
="11"
></
td
>
</
tr
>
</
table
>
</
FONT
>
</
form
>
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<
script
language
="javascript"
>
var bUploaded=false;//是否已经上传的标志
var bIsRename=false;
function remove()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var strRemoveFile = Form1.LstFileList.options[Form1.LstFileList.selectedIndex].innerHTML;
var strFileList = "^" + Form1.hidFileList.value + "^";
strRemoveFile = "^" + strRemoveFile + "^";
var nIndex = strFileList.indexOf(strRemoveFile);
if( nIndex > -1 )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strFileList = strFileList.replace(strRemoveFile,"^");
if(strFileList.length > 1)
strFileList = strFileList.substring(1,strFileList.length-1);
else
strFileList = "";
Form1.hidFileList.value = strFileList;
}
Form1.LstFileList.options.remove(Form1.LstFileList.selectedIndex);
}
//设置是否可重命名的标志
function setWritable()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if( Form1.chkUpLoadFolder.checked == true)
Form1.TxtFileName.readOnly = true;
else
Form1.TxtFileName.readOnly = false;
}
//设置是否已经上传的标志
function setFlag()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.document.all.div_message.innerHTML="<font color=red>已经做好上传的准备</font>";
bUploaded=false;
}
//上传文件
function upload()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.document.all.div_message.innerHTML="";
var strFilePath=document.all.FileUpload.value;
if(strFilePath.length <=0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
alert("请先选择要上传的文件");
return;
}
if(bUploaded)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(window.confirm("文件已经上传过,您确定要重新上传吗?")==false)
return;
}
//创建filesystemobject对象
var fso ;
fso = new ActiveXObject("Scripting.FileSystemObject");
if(!checkFileSize(fso,strFilePath))//检查文件大小是否超过限定值
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
//设置此次上传文件的任务名,此值唯一
var dNow=new Date();
varTaskName=dNow.getTime();
var strResult="";
if(document.all.chkUpLoadFolder.status==false)//上传单个文件
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(Form1.TxtFileName.value != "")
bIsRename = true;
strResult=uploadSingleFile(fso,strFilePath,varTaskName);
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strResult=uploadMultiFiles(fso,strFilePath,varTaskName);//批量上传文件
}
// 显示上传的结果
if(strResult.indexOf(" ")==-1)
document.all.div_message.innerHTML="<font color=red>已经成功上传!</font>";
else
document.all.div_message.innerHTML ="<font color=red>上传完毕,但是以下文件上传失败:"+strResult + "</font>";
bUploaded=true;
}
catch(e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
document.all.div_message.innerHTML ="<font color=red>上传文件出错,错误信息为:" +e.number+e.message + "</font>";
}
}
//判断上传文件总大小和单个文件大小是否超过限定值
function checkFileSize(fso,strFilePath)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var iMaxFileSize=document.all.hidMaxFileSize.value;//设定的最大值,以K为单位,该值由服务器端从config中获取后传给客户端
var iMaxUploadSize=document.all.hidMaxUpLoadSize.value;//批量上传所有文件的最大限定值,以K为单位
var iFileSizeCount=0;//文件的总大小,以B为单位
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(document.all.chkUpLoadFolder.status==true)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//iFileSize=fso.GetFile(strFilePath).ParentFolder.Size;//上传文件夹
var fldr = fso.GetFile(strFilePath).ParentFolder;
var fc = new Enumerator(fldr.files);
var iFileSize=0;
for (; !fc.atEnd(); fc.moveNext())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iFileSize=fso.GetFile(fc.item()).Size ;
if(iFileSize>(iMaxFileSize*1024))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
alert("上传的单个文件大小不能超过"+Math.floor(iMaxFileSize/1024)+"M,文件 "+fso.GetFile(fc.item()).Path +" 的大小超过限定值!");
return false;
}
iFileSizeCount=iFileSizeCount+iFileSize;//计算所有文件的总大小,以B为单位
if(iFileSizeCount>iMaxUploadSize*1024)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
alert("上传文件的总大小不能超过"+Math.floor((iMaxUploadSize/1024))+"M!");
return false;
}
}
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iFileSizeCount =fso.GetFile(strFilePath).Size;//上传单个文件
if(iFileSizeCount>(iMaxFileSize*1024))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
alert("上传的单个文件大小不能超过"+Math.floor(iMaxFileSize/1024)+"M,文件 "+fso.GetFile(strFilePath).Path +" 的大小超过限定值!");
return false;
}
}
return true;
}
catch(e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw e;
}
}
//上传单个文件,成功上传返回"",否则返回上传出错的文件名
function uploadSingleFile(fso,strFilePath,varTaskName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 创建包含默认头信息和根节点的 XML文档
var xml_dom = createXMLDOM(varTaskName);
//创建一个新节点,存储文件的二进制数据
var filenode=getFileNode(xml_dom,strFilePath,fso);
//判断写入文件的xml大小是否超过限定值
if((xml_dom.xml.length+filenode.xml.length)>document.all.hidMaxFileSize.value*1024)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
alert("上传的文件格式化为上传数据时超过了最大限定值"+Math.floor(iMaxFileSize/1024)+"M!");
return strFilePath;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
xml_dom.documentElement.appendChild(filenode);
Form1.LstFileList.options.add(new Option(filenode.getAttribute('filename')),Form1.hidPrjID.value);
var strFileList = Form1.hidFileList.value;
if( strFileList == "" )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Form1.hidFileList.value = filenode.getAttribute('filename');
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Form1.hidFileList.value = Form1.hidFileList.value + "^" + filenode.getAttribute('filename');
}
return postXML(xml_dom);
}
}
catch(e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw e;
}
}
//批量上传文件,此函数递归调用,返回值为未能成功上传的文件列表
function uploadMultiFiles(fso,strFilePath,varTaskName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 创建包含默认头信息和根节点的 XML文档
var xml_dom = createXMLDOM(varTaskName);
var bBatchUpload=false;//是否需要分批上传
var fc=new Enumerator(fso.GetFile(strFilePath).ParentFolder.files);//需要上传的文件数组
//获取已经上传的文件列表
var strUploadedFiles="";
if(arguments.length>3)
strUploadedFiles=arguments[3];
var strNowUpLoadFiles="";//此次上传的文件列表
var strFailFiles="";//上传失败的文件列表
for (; !fc.atEnd(); fc.moveNext())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var fle=fso.GetFile(fc.item());
re = new RegExp(fle.Name,"i"); // 创建正则表达式对象。
if(strUploadedFiles==""||strUploadedFiles.search(re)==-1)//判断文件是否已经上传过
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strNowUpLoadFiles=strNowUpLoadFiles+"    "+fle.Name;
//创建一个新节点,存储文件的二进制数据,超出限定大小时返回false
var filenode=getFileNode(xml_dom,fc.item(),fso);
//判断把单个文件xml后其大小是否超过限定值,超过限定值忽略上传该文件
if((filenode.xml.length+256)>document.all.hidMaxFileSize)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strFailFiles=strFailFiles+"    "+fle.Name+"/格式化为上传格式后大小超过限定/";
strUploadedFiles=strUploadedFiles+"*"+fle.Name;
continue;
}
//判断整个已经写入文件数据的xml大小是否超过限定值,超过限定值分批上传
if((xml_dom.xml.length+filenode.xml.length)>document.all.hidMaxFileSize.value*1024)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bBatchUpload=true;
break;
}
xml_dom.documentElement.appendChild(filenode);
Form1.LstFileList.options.add(new Option(filenode.getAttribute('filename')),Form1.hidPrjID.value);
var strFileList = Form1.hidFileList.value;
if( strFileList == "" )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Form1.hidFileList.value = filenode.getAttribute('filename');
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Form1.hidFileList.value = Form1.hidFileList.value + "^" + filenode.getAttribute('filename');
}
strUploadedFiles=strUploadedFiles+"*"+fle.Name;//记录已经上传的文件名
}
}
strFailFiles=postXML(xml_dom);//返回服务器端接收失败的文件列表
if(strFailFiles=="-1")//服务器端接收时发生异常
strFailFiles=strNowUpLoadFiles;
if(bBatchUpload)
strFailFiles=strFailFiles+uploadMultiFiles(fso,strFilePath,varTaskName,strUploadedFiles);//递归上传
return strFailFiles;
}
catch(e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw e;
}
}
//创建用于承载商船文件的xml
function createXMLDOM(varTaskName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 创建包含默认头信息和根节点的 XML文档
var xml_dom = new ActiveXObject("MSXML2.DOMDocument");
xml_dom.loadXML('<uploadfiles></uploadfiles>');
// 指定数据类型
xml_dom.documentElement.setAttribute("xmlns:dt", "urn:schemas-microsoft-com:datatypes");
xml_dom.documentElement.setAttribute("uploadtaskname",varTaskName);
return xml_dom;
}
catch(e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw e;
}
}
// 创建一个新节点,存储文件的二进制数据
//返回该文件节点实例
function getFileNode(xml_dom,strFilePath,fso)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var filenode = xml_dom.createElement("filedata");
var objNewAtt;
//创建文件名属性
objNewAtt=xml_dom.createAttribute("filename");
var strNewFilePath = "";
if(bIsRename == true)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var strNewFileName = Form1.TxtFileName.value;
var strOldFileName = strFilePath.substring(strFilePath.lastIndexOf("\\")+1, strFilePath.length);
strNewFilePath = strFilePath.replace(strOldFileName, strNewFileName);
objNewAtt.text=fso.GetFileName(strNewFilePath);
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
objNewAtt.text=fso.GetFileName(strFilePath);
}
filenode.attributes.setNamedItem(objNewAtt);
// 创建 ADO-stream 对象
var ado_stream = new ActiveXObject("ADODB.Stream");
filenode.dataType = "bin.base64";
// 打开Stream对象,读源文件
ado_stream.Type = 1; // 1=adTypeBinary
ado_stream.Open();
ado_stream.LoadFromFile(strFilePath);
// 将文件内容存入XML节点
filenode.nodeTypedValue = ado_stream.Read(-1); // -1=adReadAll
ado_stream.Close();
ado_stream=null;
return filenode;
}
catch(e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw e;
}
}
//向服务器发送xml
//返回服务器端给出的上传结果(如果服务器端接收文件时发生异常返回-1,否则返回未能成功接收的文件名列表)
function postXML(xml_dom)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 把XML文档发送到Web服务器
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("POST","Attachment.aspx",false);
xmlhttp.send(xml_dom);
var strFailFiles=xmlhttp.ResponseText;// 获取服务器返回的信息
strFailFiles=strFailFiles.substr(0,strFailFiles.indexOf("<"));
xml_dom=null;
xmlhttp=null;
return strFailFiles;
}
catch(e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw e;
}
}
</
script
>
</
body
>
</
HTML
>
aspx.cs:
using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Web;
using
System.Web.SessionState;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls;
using
System.IO;
using
System.Configuration;
namespace
Common
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public partial class Attachment : System.Web.UI.Page
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
System.Xml.XmlNodeReader xr = null;
System.Xml.XmlDocument xml_dom=null;
System.Xml.XmlNodeList xml_files=null;
System.IO.FileStream fs =null;
protected void Page_Load(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(!this.IsPostBack)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Web.Configuration.HttpRuntimeSection config = System.Web.Configuration.WebConfigurationManager.GetWebApplicationSection("system.web/httpRuntime") as System.Web.Configuration.HttpRuntimeSection;
int iMaxFileSize = config.MaxRequestLength;//从web.config中获取上传文件大小的最大允许值,以K为单位
int iMaxUpLoadSize = Convert.ToInt32(ConfigurationSettings.AppSettings.Get("MaxUpLoadSize"));
if (!IsPostBack)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//通知客户端上传单个文件和一次上传文件的最大限定值,以K为单位,因为网页本身也占空间
hidMaxFileSize.Value = iMaxFileSize.ToString();
//通知客户端批量上传所有文件的最大限定值,以K为单位
hidMaxUpLoadSize.Value = Convert.ToString(Math.Max(iMaxFileSize, iMaxUpLoadSize));
// 通知客户端项目PID
ProjectInfo currProject = (ProjectInfo)Session["CurrProject"];
hidPrjID.Value = currProject.PID.ToString();
}
if (this.Request.ContentLength <= 0)
return;
//检测接受数据的合法性
if (this.Request.ContentLength > iMaxFileSize*1024)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write("数据文件大小超出限制!");
return;
}
//从Request对象创建 XMLDOM对象
xml_dom = new System.Xml.XmlDocument();
xml_dom.Load(this.Request.InputStream);
System.Xml.XmlElement xml_root = xml_dom.DocumentElement;//获取此次上传任务,客户端把一次上传任务分成多次上传
string strRootBufferPath = GetTempFilePath(xml_root.Attributes["uploadtaskname"].Value );
// 读出包含二进制数据的节点(也就是用来存储文件数据的节点)
xml_files = xml_dom.SelectNodes("uploadfiles/filedata");
string strSaveFailFiles="";
for (int i = 0; i < xml_files.Count; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string strFileName = "";
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Xml.XmlNode xml_file = xml_files[i];
//获取文件的名称、扩展名
if( !this.chkUpLoadFolder.Checked && !this.TxtFileName.Value.Equals("") )
strFileName = this.TxtFileName.Value;
else
strFileName = xml_file.Attributes["filename"].Value;
string strFilePath = GetUniqueFilePath(strRootBufferPath, strFileName);
if (strFilePath == "")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strSaveFailFiles += "    " + strFileName;
continue;
}
//读取文件内容并写到磁盘上
byte[] filebyte = new byte[1024];
filebyte = Convert.FromBase64String(xml_file.FirstChild.InnerText);
int iReadCount = 0;
fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
while (iReadCount < filebyte.Length)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((filebyte.Length - iReadCount) > 1024)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fs.Write(filebyte, iReadCount, 1024);
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fs.Write(filebyte, iReadCount, (filebyte.Length - iReadCount));
}
iReadCount = iReadCount + 1024;
}
fs.Flush();
fs.Close();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////读取文件内容并写到磁盘上 --- VS2005下可用
//byte[] filebyte = new byte[1024];
//xr = new System.Xml.XmlNodeReader(xml_file.FirstChild);
//xr.MoveToContent();//这句必须有,表示准备读取内容
//int iReadCount = 0;
//fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
//do
//{
// iReadCount = xr.ReadContentAsBase64(filebyte, 0, 1024);
// if (iReadCount > 0)
// {
// fs.Write(filebyte, 0, iReadCount);
// fs.Flush();
// }
//} while (iReadCount > 0);
}
catch (Exception)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strSaveFailFiles += "    " + strFileName;
}
}
//向浏览器返回上传失败的文件列表
if (strSaveFailFiles != "")
Response.Write(strSaveFailFiles);
}
catch (Exception ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write(ex); //表示全部上传失败
}
finally
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//释放资源
xml_files = null;
xml_dom = null;
if (fs != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fs.Close();
fs = null;
}
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
//删除文件
protected void CmdDelFile_ServerClick(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//此处写代码通过判断前台Hide控件的值来做是否保存文件的操作
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获得临时文件存储路径,规则是网站路径下的TempFile\UserID开启新任务的时间刻度
/// </summary>
/// <param name="strUploadTask">此次上传文件任务的名称,以其作为接收文件的根目录</param>
/// <returns>根据上传任务名称返回接收文件的根目录</returns>
private string GetTempFilePath(string strUploadTask)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int iUserID = 100;//此处可在session中动态获取
string strTempPath = Server.MapPath("../TempFile")+ "\\";
//清理垃圾数据,逻辑是:清除一天前创建的文件夹
if (Directory.Exists(strTempPath))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string[] astrPath = Directory.GetDirectories(strTempPath);
for (int i = 0; i < astrPath.Length; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//根据web.config配置的上传的文件保留的天数清理文件
int iTempFileKeepDays =Convert.ToInt32(ConfigurationSettings.AppSettings.Get("TempFileKeepDays"));
if (Directory.GetCreationTime(astrPath[i]).AddDays(iTempFileKeepDays) < DateTime.Now)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Directory.Delete(astrPath[i], true);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch
{ }
}
}
}
string strFilePath = strTempPath +iUserID.ToString()+ strUploadTask +"\\";
if (!Directory.Exists(strFilePath))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Directory.CreateDirectory(strFilePath);
}
return strFilePath;
}
catch (Exception e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//MessageToUser(e.ToString());
return null;
}
finally
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获得指定路径下的唯一文件名
/// </summary>
/// <param name="strBufferPath">存放文件的路径</param>
/// <param name="strFileName">文件名称</param>
/// <returns>返回全路径文件名</returns>
private string GetUniqueFilePath(string strBufferPath, string strFileName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (strBufferPath.Substring(strBufferPath.Length - 1) != "\\")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strBufferPath += "\\";
}
if (Directory.Exists(strBufferPath) == false)
Directory.CreateDirectory(strBufferPath);
int i = 1;
while (File.Exists(strBufferPath +strFileName))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
i++;
strFileName =Path.GetFileNameWithoutExtension(strFileName) +"("+ i.ToString() + ")" +Path.GetExtension(strFileName);
}
return strBufferPath+strFileName;
}
catch (Exception e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "";
}
}
}
}