using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Text.Json; using System.Text.Json.Serialization; namespace ZUGFeRDRESTService.Controllers { [Route("api/[controller]")] [ApiController] public class ZugferdValidationController : ControllerBase { public static string RESPONSE_OK = "OK"; public static string RESPONSE_ERROR = "ERROR"; private readonly IZugferdValidationDataService _dataService; public class ZugferdValidationResponse { public string status; public string message; public List errors; public ZugferdValidationResponse() { status = RESPONSE_OK; message = String.Empty; errors = new List(); } } public ZugferdValidationController(IZugferdValidationDataService dataService) { _dataService = dataService; } /// /// POST: api/ZugferdValidation /// /// This parameter's name needs to correspond to the html form's file-input name [HttpPost] public async Task Post(List files) { var oFilePaths = new List(); var oFileNames = new List(); if (files.Count == 0) { return new ZugferdValidationResponse() { status = RESPONSE_ERROR, message = "No File received!" }; } foreach (var formFile in files) { var oFilePath = Path.GetTempFileName(); oFilePaths.Add(oFilePath); oFileNames.Add(formFile.FileName); using (var oStream = new FileStream(oFilePath, FileMode.Create)) { await formFile.CopyToAsync(oStream); } } var oResponse = new ZugferdValidationResponse { message = "You uploaded the following file: " + oFileNames.First() }; return oResponse; } } }