最近公司项目需要做多文件上传处理,同时要求对上传上来的数据在不插入数据库的情况进行操作。
根据实际情况我做了如下的处理:
1:先实现把文件放在DataTable中。
2:将DataTable中数据放在DataGrid中进行呈现(可以及时的查看附件,可以及时的更改)。
3:启动事务,处理附件的主数据(某条具有多附件信息的数据)保存主数据并且返回唯一标识。
4:接受事务参数,保存附件信息。
5:返回。
在这其中,对于多文件上传的问题,我参考了老猫的多附件上传的代码。代码如下
根据实际情况我做了如下的处理:
1:先实现把文件放在DataTable中。
2:将DataTable中数据放在DataGrid中进行呈现(可以及时的查看附件,可以及时的更改)。
3:启动事务,处理附件的主数据(某条具有多附件信息的数据)保存主数据并且返回唯一标识。
4:接受事务参数,保存附件信息。
5:返回。
在这其中,对于多文件上传的问题,我参考了老猫的多附件上传的代码。代码如下
private void SaveFiles()
{
///遍历file
HttpFileCollection files = HttpContext.Current.Request.Files;
///状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder(1000);
strMsg.Append("上传的文件分别是:<hr color=red>");
try
{
for(int iFile = 0; iFile < files.Count; iFile++)
{
///检查文件扩展名字
///提供访问客户端已经上载的各个文件
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + fileName + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("info/") + fileName);
}
}
strStatus.Text = strMsg.ToString();
return ;
}
catch(System.Exception Ex)
{
throw new Exception(Ex.Message);
}
}
{
///遍历file
HttpFileCollection files = HttpContext.Current.Request.Files;
///状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder(1000);
strMsg.Append("上传的文件分别是:<hr color=red>");
try
{
for(int iFile = 0; iFile < files.Count; iFile++)
{
///检查文件扩展名字
///提供访问客户端已经上载的各个文件
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + fileName + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("info/") + fileName);
}
}
strStatus.Text = strMsg.ToString();
return ;
}
catch(System.Exception Ex)
{
throw new Exception(Ex.Message);
}
}