2022-11-10 14:34:53 +08:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2022-12-09 15:37:15 +08:00
|
|
|
|
using NPOI.SS.Formula.Functions;
|
2022-11-10 14:34:53 +08:00
|
|
|
|
using Repository.BackendRepository.Interface;
|
|
|
|
|
using Repository.FrontendRepository.Interface;
|
|
|
|
|
using System;
|
2022-12-09 15:37:15 +08:00
|
|
|
|
using System.Collections.Generic;
|
2022-12-07 09:25:43 +08:00
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
2022-11-10 14:34:53 +08:00
|
|
|
|
using System.IO;
|
2022-12-07 09:25:43 +08:00
|
|
|
|
using System.Net;
|
2022-12-14 17:34:13 +08:00
|
|
|
|
using System.Web;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Http.Features;
|
|
|
|
|
using NPOI.HPSF;
|
2022-11-10 14:34:53 +08:00
|
|
|
|
|
|
|
|
|
namespace FrontendWebApi.ApiControllers
|
|
|
|
|
{
|
|
|
|
|
public class FileController : MyBaseApiController<GraphManageController>
|
|
|
|
|
{
|
|
|
|
|
private readonly IBackendRepository backendRepository;
|
|
|
|
|
private readonly IFrontendRepository frontendRepository;
|
|
|
|
|
|
|
|
|
|
private string graphManageFileSaveAsPath = "";
|
|
|
|
|
private string operationFileSaveAsPath = "";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public FileController(IBackendRepository backendRepository, IFrontendRepository frontendRepository)
|
|
|
|
|
{
|
|
|
|
|
this.backendRepository = backendRepository;
|
|
|
|
|
this.frontendRepository = frontendRepository;
|
|
|
|
|
graphManageFileSaveAsPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "graph_manage");
|
|
|
|
|
operationFileSaveAsPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "operation");
|
|
|
|
|
}
|
2022-12-14 17:34:13 +08:00
|
|
|
|
public ActionResult Download([FromQuery]string type , string savename)
|
2022-11-10 14:34:53 +08:00
|
|
|
|
{
|
|
|
|
|
var path = "";
|
|
|
|
|
if (type == "graph")
|
|
|
|
|
{
|
|
|
|
|
path = Path.Combine(graphManageFileSaveAsPath, savename);
|
|
|
|
|
}
|
|
|
|
|
else if (type == "operation")
|
|
|
|
|
{
|
|
|
|
|
path = Path.Combine(operationFileSaveAsPath, savename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (System.IO.File.Exists(path) && path != "")
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-12-14 17:34:13 +08:00
|
|
|
|
var auth = HttpContext.Request.Headers["Authorization"];
|
|
|
|
|
var token = auth.ToString()?.Split("Bearer ")[1];
|
|
|
|
|
var jwt = new JwtSecurityTokenHandler().ReadJwtToken(token);
|
|
|
|
|
if (jwt == null)
|
|
|
|
|
return Unauthorized(HttpStatusCode.Unauthorized);
|
|
|
|
|
else if (!System.IO.File.Exists(path))
|
|
|
|
|
return NotFound("找不到文件");
|
2022-11-10 14:34:53 +08:00
|
|
|
|
|
2022-12-14 17:34:13 +08:00
|
|
|
|
var bufferingFeature = HttpContext.Features.Get<IHttpResponseBodyFeature>();
|
|
|
|
|
bufferingFeature?.DisableBuffering();
|
|
|
|
|
var content = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
|
|
|
var response = File(content, "application/octet-stream");//FileStreamResult
|
|
|
|
|
|
|
|
|
|
return response;
|
2022-11-10 14:34:53 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
2022-12-14 17:34:13 +08:00
|
|
|
|
{ return null; }
|
2022-11-10 14:34:53 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
2022-12-14 17:34:13 +08:00
|
|
|
|
return null;
|
2022-11-10 14:34:53 +08:00
|
|
|
|
}
|
2022-12-07 09:25:43 +08:00
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("api/df")]
|
|
|
|
|
public ActionResult DownloadFile(string path, string fileName, string token)
|
|
|
|
|
{
|
|
|
|
|
var jwt = new JwtSecurityTokenHandler().ReadJwtToken(token);
|
|
|
|
|
if (jwt == null)
|
|
|
|
|
return Unauthorized(HttpStatusCode.Unauthorized);
|
|
|
|
|
else if (fileName == null)
|
|
|
|
|
return NotFound("找不到文件");
|
|
|
|
|
|
|
|
|
|
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "excel", path);
|
|
|
|
|
return File(System.IO.File.ReadAllBytes(Path.Combine(filePath, fileName)), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
|
|
|
|
|
}
|
2022-12-09 15:37:15 +08:00
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("api/Upload")]
|
|
|
|
|
public ActionResult DroZonUplFile()
|
|
|
|
|
{
|
|
|
|
|
var a = HttpContext.Request.Form.Files;
|
|
|
|
|
return Json(new
|
|
|
|
|
{
|
|
|
|
|
code = "0000",
|
|
|
|
|
data = HttpContext.Request.Form.Files
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-11-10 14:34:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|