jquery ajax 上传文件

html
            //获取客户端上传的文件集合
            HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            //相对路径
            string path = "";
            //判断文件是否存在
            if (files.Count > 0) {
                //获取扩展名
                string extension = Path.GetExtension(files[0].FileName);
                if (!string.IsNullOrEmpty(extension))
                {
                    path = "/Content/images/" + Guid.NewGuid() + extension;
                    //获取文件集合中的第一个文件(每次只上传一个文件)
                    HttpPostedFile file = files[0];
                    string fullpath = System.Web.HttpContext.Current.Server.MapPath(path);
                    file.SaveAs(fullpath);
                }
            }
后台代码
     $.ajaxFileUpload({
            url: '/MarketingStrategyModule/MarketingStrategy/SaveMarketingBookForm?keyValue=' + keyValue,
            data: {
                name: entity.name, rule_rights: entity.rule_rights,
                state: entity.state, is_member: entity.is_member, start_date: entity.start_date,
                end_date: entity.end_date, dsc: entity.dsc, terminal: entity.terminal, members: entity.members,
                products: entity.products.replace(new RegExp('"', "gm"), "'")  //entity.products是一个数组,需要先转成JSON后,再进行正则替换,否则后台接受到的是“【{”
            },
            type: "POST",
            fileElementId: 'uploadFile',
            dataType: 'json',
            success: function (data) {
                bpm.loading(false);
                if (data.code == 200) {
                    //保存成功后才回调
                    if (!!callBack) {
                        callBack();
                    }
                    bpm.alert.success('保存成功');
                    bpm.layerClose(window.name);
                } else {
                    bpm.alert.warning(data.info);
                }
            }
        });
View Code
原文地址:https://www.cnblogs.com/xielideboke/p/11136641.html