版权声明:本文为博主原创文章,未经博主允许不得转载。
1.页面
<head runat="server">
<title>上传文件</title>
<!--#include file="/_Layouts/include/Common.htm" -->
<link href="/_Layouts/res/css/ahtf.css" rel="stylesheet" type="text/css" />
<script src="../res/js/calendarCN.js" type="text/javascript"></script>
<style type="text/css">
.style1
{
width: 50px;
}
</style>
<script type="text/javascript">
var appId,
appType,
aDefId; $(function () {
appType = GetQueryString("appType");
aDefId = GetQueryString("activityId");
appId = GetQueryString("appId");
});
//获取请求参数
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr().match(reg);
if (r != null) return decodeURI(r[]); return null;
} //新增一行
function insertRows() {
var tb = $("#tbUpload"); //找到table的ID
var rowNum = $("#tbUpload tr").size(); //获取表格的行数
var tempRow = rowNum - ; ; //用于新增tr、td、属性下标 var $tdEm = $("<td></td>"); //创建一个td
var $tdFile = $("<td></td>"); //创建一个td
$tdEm.html(""); //空的
$tdFile.html("<input type =\'file\' id=\'UploadAttachment" + (tempRow + ) + "\' name=\'UploadAttachment" + (tempRow + ) + "\' value=\'\' style=\'width: 450px;\'/>"
+ " <a href=\'#\' name=\'teams[" + tempRow + "].del\' onclick=\'delRows(" + tempRow + ")\'>删除</a>"); //文件上传控件和删除按钮
var $tr = $("<tr id=\'tr_" + tempRow + "\' ></tr>"); // 创建tr,将td放置到tr中
$tr.append($tdEm);
$tr.append($tdFile);
//在表格的最后追加新增的tr
tb.append($tr);
} //删除某行
function delRows(sub) {
var rowNum = sub + ; //行号=下标+1
var tb = $("#tbUpload"); //找到table的ID
var tempRow = $("#tbUpload tr").size(); //获取表格的行数
if (tempRow > rowNum) {
$("#tr_" + sub).remove(); //删除tr
for (i = (parseInt(sub) + ); i < tempRow - ; i++) { //把删除的行后面的列及元素都向前挪一位
$("#tr_" + i).attr("id", "tr_" + (i - ) + "");
$("input[name='UploadAttachment" + (i + ) + "']").attr("name", "UploadAttachment" + i);
$("a[name='teams[" + i + "].del").attr("onclick", "delRows(" + (i - ) + ")");
$("a[name='teams[" + i + "].del").attr("name", "teams[" + (i - ) + "].del");
}
}
} //保存上传的文件
function saveAttachment() {
var count = $("#tbUpload tr").size();
$.ajaxFileUpload
(
{
url: '/_Layouts/wf/Controller/JyzdcgtzgtzdWfManager.ashx?method=SaveAttachment', //用于文件上传的服务器端请求地址
type: 'POST',
data: { appId: appId, aDefId: aDefId, appType: appType, count: count },
secureuri: false, //一般设置为false
fileElementId: ['UploadAttachment0'], //'UploadAttachment1', 'UploadAttachment2', 'UploadAttachment3', 'UploadAttachment4', 'UploadAttachment5', 'UploadAttachment6'], //文件上传空间的id属性
dataType: 'JSON', //返回值类型 一般设置为json
success: function (data) //服务器成功响应处理函数
{
data = $.parseJSON(data);
if (data.success) {
window.opener.SearchAttachment(appId, aDefId, "0,1", data.fileId);
$.messager.alert('提示', data.message, 'info');
window.close();
} else {
$.messager.alert('提示', data.message, 'error');
}
}
}
);
} </script>
</head>
<body>
<form id="form1" runat="server">
<input id="AppId" type="hidden" value="" />
<div>
<table id="tbUpload" border="" style="width: 800px; text-align: left; font-size: 12px;">
<tr id="trFirst" style="height: 35px">
<td class="style1">
上传文件
</td>
<td>
<input type="file" name="UploadAttachment0" id="UploadAttachment0" style="width: 450px;" />
<a href="#" onclick="insertRows();">添加</a> <span style="font-weight: bold">
<a id="UploadAttachment_btnSubmit" href="javaScript:void(0)" onclick="saveAttachment()">
上传</a></span>
<%--<a id="Span1" href="#UploadAttachment_btnSubmit"
style="color: red; ">请选择要上传的文件</a> 关于锚链接 的实验 感觉跟label for差不多--%>
<span id="UploadAttachment_RequiredFieldValidator1" style="color: red; display: none;">
请选择要上传的文件</span>
</td>
</tr>
</table>
<%--上传文件:<input type="file" name="UploadAttachment1$FileUpload" id="UploadAttachment1_FileUpload" style="width:450px;">
添加 上传--%>
</div>
</form>
</body>
</html>
后台SaveAttachment方法:
#region 保存上传的文件 modify by wh 2017022
//[WNet("WNet1")]
public void SaveAttachment(HttpContext context)
{
try
{
#region 参数
string appId = context.Request["appId"];
string aDefId = context.Request["aDefId"];
string appType = context.Request["appType"];
int count = int.Parse(context.Request["count"]);
#endregion string fileId = string.Empty;
int flag = ; //context.Request.Files.AllKeys //接收上传后的文件
for (int i = ; i < count; i++)
{
HttpPostedFile file = context.Request.Files["UploadAttachment" + i + ""];
//获取文件的文件名(不包括文件的路径)
if (file == null)
{
continue;
}
string fileName = file.FileName;
if (!string.IsNullOrEmpty(fileName))
{
string path = AppHelper.GetSysPath(SysFolder.Lgfz);
if (appType == "JYZDCGTZG")
{
path += "/Jyzdcgt/";
}
else if (appType == "JYZYCSXSB")
{
path += "/Jyzycsx/";
}
else if (appType == "YKDCGTZG")
{
path += "/Ykdcgt/";
}
// 判断路径是否存在,不存在则新建路径
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
} FileModel fileInf = new FileModel();
string guid = Guid.NewGuid().ToString();
string filePath = path + guid + "_" + Path.GetFileName(fileName);
//保存文件
file.SaveAs(filePath); fileInf.FileId = guid;
fileInf.FileName = Path.GetFileName(file.FileName);//获取文件的文件名(不包括文件的路径)
fileInf.FileAddress = path + guid + "_" + fileName;//文件存放地址
fileInf.ActivityId = aDefId;
fileInf.AppId = appId;
fileInf.CreateUser = CurrentUser.User.UserAlias;
fileInf.LastModifyUser = CurrentUser.User.UserAlias;
fileInf.Status = "";//status为0——新增未保存 // 保存文件信息到数据库
Dao.SaveAttachment(fileInf);
flag++;
fileId += fileInf.FileId;
if (i != count - ) {
fileId += ",";
}
}
}
if (flag > )
{
Dictionary<string, object> map = new Dictionary<string, object>();
map["success"] = true;
map["message"] = "文件上传成功!";
map["fileId"] = fileId;// fileInf.FileId;
context.Response.Write(JsonHelper.getBeanJson(map));
}
else
{
Dictionary<string, object> map = new Dictionary<string, object>();
map["success"] = false;
map["message"] = "文件为空!";
context.Response.Write(JsonHelper.getBeanJson(map));
}
}
catch (Exception ex)
{
Dictionary<string, object> map = new Dictionary<string, object>();
map["success"] = false;
map["message"] = "文件上传失败!";
context.Response.Write(JsonHelper.getBeanJson(map));
Log.Error("保存文件信息异常:" + ex.Message);
}
}
#endregion
应该就没了