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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM