首页 > 代码库 > jq表单上传多文件 前后台代码

jq表单上传多文件 前后台代码

Html>>>>>>>>>

  <form id="Job_Notice_Form"  method="post" enctype="multipart/form-data">

<div id="f" >
<div id="zhi"> <div style="display:none">
<div id="content">
<input id="File1" type="file" name="file" style=" width:450px;" />&nbsp;
<a id="jian" href="http://www.mamicode.com/#" class="easyui-linkbutton" data-options="iconCls:‘icon-delete‘">删除</a>
<br />
</div>
</div>
</div>
</div>
<a id="tianjia" href="http://www.mamicode.com/#" class="easyui-linkbutton" data-options="iconCls:‘icon-add‘">添加</a>

<a id="tianjia" href="http://www.mamicode.com/#" class="easyui-linkbutton" onclik="Save()" data-options="iconCls:‘icon-Save‘">保存</a>

 

</form>

JS>>>>>>

<script type="text/javascript">
$(function () {
//跟添加按钮绑定点击事件
$("#tianjia").bind("click", function () {
//克隆id=content下面的元素。添加到id=zhi的div里面
$("#content").clone(true).appendTo("#zhi");

});

//删除file控件操作
$("#jian").click(function () {
//查找上传控件的个数
var shu = $("#zhi").find("#content").size();
//如果有多,就删除掉

$(this).parent().remove();

});
});
function DelFile(id) {
$("#zhi").append(‘ <input name="DelFile" value="http://www.mamicode.com/‘ + id + ‘" type="hidden" />‘)

}

 

funtion Save()

{

//----上传文件

top.$("#Job_Notice_Form").form(‘submit‘, {
url: "/View/Admin/App/Ashx/Job_File_Handler.ashx?action=add",
onSubmit: function () {

},
success: function (data) {
var d = eval(‘(‘ + data + ‘)‘);
if (d.Success) {
var i = d.Data;
alert(d.Data);

}
else {
msg.warning(data.Message);
}

}
})

 


//-End-上传文件

}
</script>

后台》》》》》》

string action = context.Request["action"];
switch (action)
{

case "add":
string FileIdSet = "";

if (context.Request.Files.Count > 0)
{

for (int i = 0; i < context.Request.Files.Count; i++)
{

Job_File file = new Job_File();
HttpPostedFile hpFile = context.Request.Files[i];
if (!String.IsNullOrEmpty(hpFile.FileName))
{
string ext = System.IO.Path.GetExtension(hpFile.FileName);
if (hpFile.ContentType != "image/jpeg" || hpFile.ContentType != "image/pjpeg")
{

file.FileType = 1;
//给文件取随及名
Random ran = new Random();
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ran.Next(100, 1000) + ext;
//保存文件
string path = context.Request.MapPath(fileName);
file.FileName = hpFile.FileName.Substring(hpFile.FileName.LastIndexOf("\\") + 1);
string uriString = System.Web.HttpContext.Current.Server.MapPath("~/View/UpLoad/App/").ToString();
file.FilePath = "/View/UpLoad/App/" + path.Substring(path.LastIndexOf("\\") + 1);
hpFile.SaveAs(uriString + file.FilePath.Substring(file.FilePath.LastIndexOf("/") + 1));
//提示上传成功
}

/*添加一条信息;*/
int res = Job_File_DAL.Instance.Insert(file);
FileIdSet += res + ",";
}
}
}

if (!String.IsNullOrEmpty(FileIdSet))
{
JsonMsg = new JsonMessage
{
Success = true,
Data = http://www.mamicode.com/FileIdSet.Substring(0, FileIdSet.Length-1),
Message = "添加成功"
}.ToString();
}
else
{
JsonMsg = new JsonMessage
{
Success = false,
Data = "http://www.mamicode.com/0",
Message = "添加失败"
}.ToString();
} context.Response.Write(JsonMsg);
context.Response.End();
break;}