95 lines
3.7 KiB
C#
95 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;
|
|
|
|
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 bool Download([FromQuery]string type , string savename, string oriname)
|
|
{
|
|
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
|
|
{
|
|
FileInfo xpath_file = new FileInfo(path); //要 using System.IO;
|
|
|
|
// 將傳入的檔名以 FileInfo 來進行解析(只以字串無法做)
|
|
HttpContext.Response.Clear(); //清除buffer
|
|
HttpContext.Response.ContentType = "application/octet-stream";
|
|
// 檔案類型還有下列幾種"application/pdf"、"application/vnd.ms-excel"、"text/xml"、"text/HTML"、"image/JPEG"、"image/GIF"
|
|
HttpContext.Response.Headers.Add("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(oriname, System.Text.Encoding.UTF8));
|
|
// 考慮 utf-8 檔名問題,以 out_file 設定另存的檔名
|
|
HttpContext.Response.Headers.Add("Content-Length", xpath_file.Length.ToString()); //表頭加入檔案大小
|
|
HttpContext.Response.SendFileAsync(xpath_file.FullName);
|
|
|
|
return true;
|
|
|
|
}
|
|
catch (Exception)
|
|
{ return false; }
|
|
|
|
}
|
|
else
|
|
return false;
|
|
}
|
|
|
|
[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
|
|
});
|
|
}
|
|
}
|
|
}
|