.net 多文件上传
版权声明:本文为博主原创文章,未经博主允许不得转载。
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
应该就没了
.net 多文件上传的更多相关文章
- jquery.uploadify文件上传组件
1.jquery.uploadify简介 在ASP.NET中上传的控件有很多,比如.NET自带的FileUpload,以及SWFUpload,Uploadify等等,尤其后面两个控件的用户体验比较好, ...
- 11、Struts2 的文件上传和下载
文件上传 表单准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设置 ...
- Java FtpClient 实现文件上传服务
一.Ubuntu 安装 Vsftpd 服务 1.安装 sudo apt-get install vsftpd 2.添加用户(uftp) sudo useradd -d /home/uftp -s /b ...
- 小兔Java教程 - 三分钟学会Java文件上传
今天群里正好有人问起了Java文件上传的事情,本来这是Java里面的知识点,而我目前最主要的精力还是放在了JS的部分.不过反正也不麻烦,我就专门开一贴来聊聊Java文件上传的基本实现方法吧. 话不多说 ...
- ,net core mvc 文件上传
工作用到文件上传的功能,在这个分享下 ~~ Controller: public class PictureController : Controller { private IHostingEnvi ...
- Web开发安全之文件上传安全
很长一段时间像我这种菜鸡搞一个网站第一时间反应就是找上传,找上传.借此机会把文件上传的安全问题总结一下. 首先看一下DVWA给出的Impossible级别的完整代码: <?php if( iss ...
- AutoIt实现Webdriver自动化测试文件上传
在运用WebDriver进行自动化测试时,由于WebDriver自身的限制,对于上传文件时Windows弹出的文件选择窗口无法控制,通过在网上查找资料锁定使用AutoIt来控制文件上传窗口. Auto ...
- Struts的文件上传下载
Struts的文件上传下载 1.文件上传 Struts2的文件上传也是使用fileUpload的组件,这个组默认是集合在框架里面的.且是使用拦截器:<interceptor name=" ...
- .JavaWeb文件上传和FileUpload组件使用
.JavaWeb文件上传 1.自定义上传 文件上传时的表单设计要符合文件提交的方式: 1.提交方式:post 2.表单中有文件上传的表单项:<input type="file" ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(32)-swfupload多文件上传[附源码]
系列目录 文件上传这东西说到底有时候很痛,原来的asp.net服务器控件提供了很简单的上传,但是有回传,还没有进度条提示.这次我们演示利用swfupload多文件上传,项目上文件上传是比不可少的,大家 ...
随机推荐
- Laravel - Property [title] does not exist on this collection instance
When you're using get() you get a collection. In this case you need to iterate over it to get proper ...
- vue 数组、对象 深度拷贝和赋值
由于此对象的引用类型指向的都是一个地址(除了基本类型跟null,对象之间的赋值,只是将地址指向同一个,而不是真正意义上的拷贝) 数组: let a = [11,22,33]; let b = a; / ...
- 时间模块(time/date)
在Python中,常用的表示方式的时间有:时间戳,字符串时间,元组时间(既年,月,日,时,分,秒,周几,一年中的第几天,时区) time模块: time.timezone: 获取当前标准时 ...
- python函数篇:名称空间、作用域和函数的嵌套
一.名称空间:(有3类) (1)内置名称空间(全局作用域) (2)全局名称空间(全局作用域) (3)局部名称空间(局部作用域) 关于名称空间的查询: x=1 def func(): print('fr ...
- 自定义UINavigationController push和pop动画
http://segmentfault.com/q/1010000000143983 默认的UINavigationController push和pop的默认动画都是左右滑动推出,我的应用要求这种界 ...
- Linux内核同步机制之(四):spin lock【转】
转自:http://www.wowotech.net/kernel_synchronization/spinlock.html 一.前言 在linux kernel的实现中,经常会遇到这样的场景:共享 ...
- phoronix-test-suite测试云服务器
centos系统 phoronix-test-suite是目前Linux下比较常用的性能测试软件. 使用phoronix-test-suite条件前提:需要安装php5,需要PHP都DOM扩展 因为是 ...
- 3.orm之peewee
peewee是一款orm框架,为什么选择peewee,是因为它比较简单和Django比较类似,而且还有一个async-peewee,可以进行异步化. 如何定义model和生成表 ''' 我们要定义两张 ...
- 使用System.getProperty("line.separator")时没有换行问题解决
项目中要实现替换模版txt文本里面的内容,然后生成新的文档,其中先把模版文本的内容通过创建的 BufferedReader bufReader 使用 readLine() 来一行一行读取,所以在完成替 ...
- rest_framework 访问频率(节流)流程
访问频率流程 访问频率流程与认证流程非常相似,只是后续操作稍有不同 当用发出请求时 首先执行dispatch函数,当执行当第二部时: #2.处理版本信息 处理认证信息 处理权限信息 对用户的访问频率进 ...