104 lines
4.1 KiB
C#
104 lines
4.1 KiB
C#
using Gather.Common.Util;
|
|
using GatherAPI.api.back;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace GatherAPI.Controllers.back
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class UploadFileController : Controller
|
|
{
|
|
private readonly IWebHostEnvironment _webHostEnvironment;
|
|
|
|
public UploadFileController(IWebHostEnvironment webHostEnvironment)
|
|
{
|
|
_webHostEnvironment = webHostEnvironment;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 不分片上传文件接口
|
|
/// </summary>
|
|
/// <param name="Files">文件</param>
|
|
/// <param name="req">非文件其它的业务参数</param>
|
|
/// <returns></returns>
|
|
[Authorize]
|
|
[HttpPost]
|
|
public async Task<JsonResult> Index([FromForm] IFormFile Files, [FromForm] UploadFileRequest req)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(req.ExamId))
|
|
{
|
|
return Json(Tool.GetJsonWithCode(APICode.Fail, "考试id不能为空"));
|
|
}
|
|
if (string.IsNullOrWhiteSpace(req.UserId))
|
|
{
|
|
return Json(Tool.GetJsonWithCode(APICode.Fail, "用户id不能为空"));
|
|
}
|
|
if (string.IsNullOrWhiteSpace(req.FileDesc))
|
|
{
|
|
return Json(Tool.GetJsonWithCode(APICode.Fail, "文件描述不能为空"));
|
|
}
|
|
|
|
//var model = achievement_bll.GetModelList(" ExamId='" + req.ExamId + "' and UserId='" + req.UserId + "'").FirstOrDefault();
|
|
//if (model == null)
|
|
//{
|
|
// return Json(Tool.GetJsonWithCode(APICode.Fail, "找不到考生考试记录"));
|
|
//}
|
|
|
|
try
|
|
{
|
|
if (Files.Length <= 0)
|
|
{
|
|
return Json(Tool.GetJsonWithCode(APICode.Fail, "文件不能为空"));
|
|
}
|
|
// Handle file upload
|
|
var fileName = Guid.NewGuid().ToString() + Path.GetExtension(Files.FileName);
|
|
var folderPath = "/Upload/PlayBack/";
|
|
if (!Directory.Exists(_webHostEnvironment.WebRootPath + folderPath))
|
|
{
|
|
Directory.CreateDirectory(_webHostEnvironment.WebRootPath + folderPath);
|
|
}
|
|
var filePath = folderPath + fileName;
|
|
var serverPath = _webHostEnvironment.WebRootPath + filePath;
|
|
using (var stream = new FileStream(serverPath, FileMode.Create))
|
|
{
|
|
await Files.CopyToAsync(stream);
|
|
}
|
|
|
|
//var video_model = new Gather.Mysql.Model.pow_video();
|
|
//video_model.VideoId = Guid.NewGuid().ToString("N");
|
|
//video_model.AchievementId = model.AchievementId;
|
|
//video_model.VideoPath = filePath;
|
|
//video_model.VideoDesc = req.FileDesc;
|
|
//video_model.CreateTime = DateTime.Now;
|
|
|
|
//if (video_bll.Add(video_model))
|
|
//{
|
|
return Json(Tool.GetJsonWithCode(APICode.Success, "文件上传成功"));
|
|
//}
|
|
//else
|
|
//{
|
|
// if (System.IO.File.Exists(serverPath))
|
|
// {
|
|
// System.IO.File.Delete(serverPath);
|
|
// }
|
|
// return Json(Tool.GetJsonWithCode(APICode.Fail, "文件上传失败"));
|
|
//}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return Json(Tool.GetJsonWithCode(APICode.Fail, e.Message));
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Util.LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace);
|
|
return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。"));
|
|
}
|
|
}
|
|
}
|
|
}
|