123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using CsvHelper;
- using CsvHelper.Configuration;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.StaticFiles;
- using Microsoft.Net.Http.Headers;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Linq;
- using Microsoft.AspNetCore.Mvc.ApiExplorer;
- namespace XYY.Core.Standard.Mvc
- {
- public class ApiControllerBase : ControllerBase
- {
- string tempPath = System.AppContext.BaseDirectory;
- [NonAction]
- public async Task<IActionResult> OutCsv<X, Y>(List<X> list, string fileName) where Y : ClassMap<X>
- {
- if (!System.IO.Directory.Exists(tempPath))
- System.IO.Directory.CreateDirectory(tempPath);
- string filePath = tempPath + "\\" + Guid.NewGuid() + ".csv";
- using (var writer = new StreamWriter(filePath, false, Encoding.GetEncoding("GB2312")))
- {
- using (var csv = new CsvWriter(writer, System.Globalization.CultureInfo.InvariantCulture))
- {
- csv.Context.RegisterClassMap<Y>();
- await csv.WriteRecordsAsync(list);
- }
- }
- var data = System.IO.File.ReadAllBytes(filePath);
- System.IO.File.Delete(filePath);
- return CSVFile(fileName, data);
- }
- [NonAction]
- public IActionResult CSVFile(string fileName, byte[] data)
- {
- this.HttpContext.Response.Headers.Add("ContentDisposition", "attachment");
- return File(data, "text/csv", fileName);
- }
- [NonAction]
- public new IActionResult File(string fileName, byte[] data)
- {
- this.HttpContext.Response.Headers.Add("ContentDisposition", "attachment");
- new FileExtensionContentTypeProvider().TryGetContentType(fileName, out var contentType);
- return File(data, contentType, fileName);
- }
- [NonAction]
- public new IActionResult Ok(object o)
- {
- return new OkObjectResult(new ApiJsonModel
- {
- code = HttpStatusCode.OK,
- data = o,
- success = true,
- message = "操作成功!"
- });
- }
- [NonAction]
- public IActionResult Ok(string message)
- {
- return new OkObjectResult(new ApiJsonModel
- {
- code = HttpStatusCode.OK,
- data = message,
- success = true,
- message = "操作成功!"
- });
- }
- [NonAction]
- public new IActionResult Ok()
- {
- return new OkObjectResult(new ApiJsonModel
- {
- code = HttpStatusCode.OK,
- data = string.Empty,
- success = true,
- message = "操作成功!"
- });
- }
- [NonAction]
- public IActionResult Error(string message)
- {
- return new OkObjectResult(new ApiJsonModel
- {
- code = HttpStatusCode.InternalServerError,
- data = message,
- success = false,
- message = "操作失败!"
- });
- }
- [NonAction]
- public IActionResult GetError(string message, object response)
- {
- return new OkObjectResult(new ApiJsonModel
- {
- code = HttpStatusCode.InternalServerError,
- data = response,
- success = false,
- message = message,
- error = message
- });
- }
- }
- }
|