using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Web;
using System.IO;
namespace swaggerTest1.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class OrderController : ControllerBase
{
[HttpGet]
public string OrderSingle()
{
return "测试1";
}
[HttpGet]
public string OrderList()
{
return "订单列表";
}
[HttpPost]
public string Upload(UserInfo model)
{
//https://localhost:44303/api/Order/Upload
//{"username":"小五","ZhaiyaoUrl":"23123","QuanwenUrl":"666"}
string dir = AppDomain.CurrentDomain.BaseDirectory;
//使用path获取当前应用程序集的执行的上级目录
dir = Path.GetFullPath("..");
//使用path获取当前应用程序集的执行目录的上级的上级目录
dir = Path.GetFullPath("../../../xiaowutemp/");
if(!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
string username = model.userName;
string ZhaiyaoUrl = model.ZhaiyaoUrl;
string QuanwenUrl = model.ZhaiyaoUrl;
string quanWenpath = dir + DateTime.Now.ToString("yyyyMMddHHmmss")+"全文.doc" ;
string zhaiyaoPath = dir + DateTime.Now.ToString("yyyyMMddHHmmss") + "摘要.doc";
byte[] quanwenByte =Convert.FromBase64String(QuanwenUrl);
byte[] zhaiyaoByte = Convert.FromBase64String(ZhaiyaoUrl);
Bytes2File(zhaiyaoByte, zhaiyaoPath);
Bytes2File(quanwenByte, quanWenpath);
string res = "success";
return res;
}
/// <summary>
/// 将byte数组转换为文件并保存到指定地址
/// </summary>
/// <param name="buff">byte数组</param>
/// <param name="savepath">保存地址</param>
public static void Bytes2File(byte[] buff, string savepath)
{
if (System.IO.File.Exists(savepath))
{
System.IO.File.Delete(savepath);
}
FileStream fs = new FileStream(savepath, FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(buff, 0, buff.Length);
bw.Close();
fs.Close();
}
public class UserInfo {
public string userName { get; set; }
public string ZhaiyaoUrl { get; set; }
public string QuanwenUrl { get; set; }
}
}
}