100 lines
3.7 KiB
C#
100 lines
3.7 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using NPOI.SS.Formula.Functions;
|
|
using Repository.BackendRepository.Interface;
|
|
using Repository.FrontendRepository.Interface;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Web;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http.Features;
|
|
using NPOI.HPSF;
|
|
|
|
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");
|
|
}
|
|
public ActionResult Download([FromQuery]string type , string savename)
|
|
{
|
|
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
|
|
{
|
|
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("找不到文件");
|
|
|
|
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;
|
|
}
|
|
catch (Exception)
|
|
{ return null; }
|
|
|
|
}
|
|
else
|
|
return null;
|
|
}
|
|
|
|
[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);
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("api/Upload")]
|
|
public ActionResult DroZonUplFile()
|
|
{
|
|
var a = HttpContext.Request.Form.Files;
|
|
return Json(new
|
|
{
|
|
code = "0000",
|
|
data = HttpContext.Request.Form.Files
|
|
});
|
|
}
|
|
}
|
|
}
|