Exemplo n.º 1
0
        public IActionResult DownloadHeaderCSVJS([FromBody] Dictionary <string, object> param)
        {
            // ログインチェック
            if (!isLogin(param))
            {
                return(Unauthorized());
            }

            var searchCondition = new UserSearchCondition();

            if (param.ContainsKey("requestData"))
            {
                // パラメータの設定
                var requestData = param["requestData"] as Newtonsoft.Json.Linq.JObject;
                searchCondition = getUserSearchCondition(requestData);
            }

            var csvData = new System.Text.StringBuilder();

            try
            {
                // データ取得とCSV文字列取得
                var models    = service.GetAllUsers(searchCondition);
                var generator = new CSVGenerator <UserModel>();
                foreach (var model in models)
                {
                    generator.Add(model);
                }
                csvData.AppendLine(generator.GetCSV(true, getDownloadColumnUserModel()));
            }
            catch (Exception ex)
            {
                logger.LogCritical("{0}", ex.Message);
                return(BadRequest());
            }

            var status  = ResponseDTO.Results.OK;
            var message = string.Empty;
            var data    = new Dictionary <string, string>();

            if (csvData.Length > 0)
            {
                data.Add("csv", csvData.ToString());

                // サンプルのファイル名
                string fileName = string.Format("テスト_{0:yyyyMMddHHmmss}.csv", DateTime.Now);
                fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
                data.Add("filename", fileName);
            }
            else
            {
                status  = ResponseDTO.Results.NG;
                message = SearchResultZero;
            }

            return(Json(new ResponseDTO(status, message, data)));
        }
Exemplo n.º 2
0
        public ActionResult DownloadHeaderCSV(string json)
        {
            var csvData = new System.Text.StringBuilder();

            try
            {
                if (!string.IsNullOrEmpty(json))
                {
                    var param = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, object> >(json);

                    // ログインチェック
                    if (!isLogin(param))
                    {
                        return(LocalRedirect("/"));
                    }

                    // 検索条件の存在確認
                    if (param.ContainsKey("requestData"))
                    {
                        var requestData = Newtonsoft.Json.JsonConvert.DeserializeObject <UserSearchCondition>(param["requestData"].ToString());

                        // データ取得とCSV文字列取得
                        var models    = service.GetAllUsers(requestData);
                        var generator = new CSVGenerator <UserModel>();
                        foreach (var model in models)
                        {
                            generator.Add(model);
                        }
                        csvData.AppendLine(generator.GetCSV(true, getDownloadColumnUserModel()));
                    }
                }
            }
            catch (Exception ex)
            {
                logger.LogCritical("{0}", ex.Message);
                return(LocalRedirect("/"));
            }

            // サンプルのファイル名
            string fileName = string.Format("テスト_{0:yyyyMMddHHmmss}.csv", DateTime.Now);

            fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);

            // ファイル名を設定
            Response.Headers.Add("Content-Disposition", "attachment; filename=" + fileName);

            return(Content(csvData.ToString(), "text/csv"));
        }