zoukankan      html  css  js  c++  java
  • 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=" 450px;" />&nbsp;
    <a id="jian" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-delete'">删除</a>
    <br />
    </div>
    </div>
    </div>
    </div>
    <a id="tianjia" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'">添加</a>

    <a id="tianjia" href="#" 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="' + id + '" type="hidden" />')

    }

     funtion Eidt()//修改

    {

    var result = $.ajax({
    url: "/View/Car/Ashx/CS_New_FileHandler.ashx",
    data: { action: 'GetFileList', IdSet: row.FileId, T: Math.floor(Math.random() * 1000000) },
    async: false
    }).responseText;
    var data = eval(result);
    if (data != null) {
    var list = "";
    $.each(data, function (i, n) {
    if (n != null) {
    list += '<div id="content">';
    list += '<a target="_blank" href="' + n.FilePath + '" style=" 350px;" >' + n.FileName + ' </a>&nbsp;';
    list += ' <a href="#" onclick=" $(this).parent().remove();" >删除</a>';
    list += ' <input name="DelFile" value="' + n.KeyId + '" type="hidden" />';
    list += ' <br />';
    list += '</div>';
    }
    });
    top.$('#zhi').append(list);
    }

    }

    funtion Save()

    {

    //----上传文件

     var FileId = "";

    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) {

    FileId = d.Data;
    var query = top.$("#CS_New_Notice_Form").serializeArray();
    var Ishave = "";
    var inputObject = top.$('#zhi input');
    for (var i = 0; i < inputObject.length; i++) {
    if ("hidden" == inputObject[i].type) {
    if (inputObject[i].value != "") {
    Ishave += inputObject[i].value + ",";
    }
    }
    }
    if (Ishave != "") {
    Ishave += FileId;
    }
    else {
    Ishave = FileId;
    }
    query.push({ name: "FileId", value: Ishave });
    var o = {};
    query = convertArray(query);
    o.jsonEntity = JSON.stringify(query);
    o.action = "Edit";
    o.KeyIds = row.CreateTime;
    o.keyid = row.KeyId;
    var data = "json=" + JSON.stringify(o);

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

    }
    })


    //-End-上传文件

    }
    </script>

    后台》》》》》》

    string action = context.Request["action"];
    string ComeFrom = "";
    if (context.Request["ComeFrom"] != null)
    {
    ComeFrom = context.Request["ComeFrom"];
    }
    string JsonMsg = ""; ;
    switch (action)
    {

    case "add":
    string FileIdSet = "";
    bool IsValid = true;//检查只能为图片,仅小于100k可以上传
    string messge = "";
    string[] Types = { ".jpg", ".jpeg", ".gif", ".bmp", ".png", ".ico" };
    int lenth = 1024 * 100;//一百k
    if (context.Request.Files.Count > 0)
    {

    for (int i = 0; i < context.Request.Files.Count; i++)
    {
    HttpPostedFile hpFile = context.Request.Files[i];
    if (!String.IsNullOrEmpty(hpFile.FileName))
    {
    string ext = System.IO.Path.GetExtension(hpFile.FileName);
    if (!Types.Contains(ext.ToLower()))
    {
    IsValid = false;
    messge = "只能为图片类型";
    break;

    }
    else if (hpFile.ContentLength > lenth)
    {
    IsValid = false;
    messge = "文件不能大于" + lenth / 1024 + "kB";
    break;
    }

    }
    }

    }
    if (context.Request.Files.Count > 0 && IsValid)
    {

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

    CS_New_File file = new CS_New_File();
    file.ComeFrom = ComeFrom;
    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 = ext.Substring(0);
    //给文件取随及名
    Random ran = new Random();
    file.FileName = hpFile.FileName.Substring(hpFile.FileName.LastIndexOf("\") + 1);
    string fileName = DateTime.Now.ToString("yyyyMMddhhmmss")+"_" + file.FileName + ext;
    //保存文件
    string path = context.Request.MapPath(fileName);

    string uriString = System.Web.HttpContext.Current.Server.MapPath("~/Upload/").ToString();
    file.FilePath = "/Upload/" + path.Substring(path.LastIndexOf("\") + 1);
    hpFile.SaveAs(uriString + file.FilePath.Substring(file.FilePath.LastIndexOf("/") + 1));
    //提示上传成功
    }


    /*添加一条信息;*/
    int res = CS_New_File_DAL.Instance.Insert(file);
    FileIdSet += res + ",";
    }
    }
    }
    if (IsValid)
    {
    if (!String.IsNullOrEmpty(FileIdSet))
    {
    JsonMsg = new JsonMessage
    {
    Success = true,
    Data = FileIdSet.Substring(0, FileIdSet.Length - 1),
    Message = "添加成功"
    }.ToString();
    }
    else
    {
    JsonMsg = new JsonMessage
    {
    Success = true,
    Data = "0",
    Message = "没有数据"
    }.ToString();
    }
    }
    else
    {
    JsonMsg = new JsonMessage
    {
    Success = false,
    Data = "0",
    Message = messge
    }.ToString();

    }

    context.Response.Write(JsonMsg);
    context.Response.End();
    break;
    case "GetFileList"://根据id集获取文件列表
    string ids = context.Request["IdSet"];
    if (!string.IsNullOrEmpty(ids))
    {
    List<CS_New_File> list = DbUtils.GetList<CS_New_File>(" select * from CS_New_File where KeyId in (" + ids + ")", null).ToList();
    context.Response.Write(JSONhelper.ToJson(list));
    }
    else
    {
    context.Response.Write("[]");
    }
    break;
    }

  • 相关阅读:
    缓存读写策略
    支撑京东小程序的开发框架 「Taro」
    Zookeeper vs Etcd
    前端开发利器 Web Replay
    kafka 中 zookeeper 具体是做什么的?
    newSQL 到底是什么?
    zookeeper配置集群
    zookeeper配置文件说明
    ssh远程访问-提示密钥不安全
    Nodejs-log4js使用配置
  • 原文地址:https://www.cnblogs.com/lucoo/p/3895197.html
Copyright © 2011-2022 走看看