using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Syncfusion.EJ2.DocumentEditor;
using System.IO;
[ApiController]
[Route("[controller]")]
public class ImportController : ControllerBase
{
[AcceptVerbs("Post")]
[HttpPost]
[EnableCors("AllowAllOrigins")]
[Route("Import")]
public string Import(IFormCollection data)
{
if (data.Files.Count == 0)
return null;
Stream stream = new MemoryStream();
IFormFile file = data.Files[0];
int index = file.FileName.LastIndexOf('.');
string type = index > -1 && index < file.FileName.Length - 1 ?
file.FileName.Substring(index) : ".docx";
file.CopyTo(stream);
stream.Position = 0;
WordDocument document = WordDocument.Load(stream, GetFormatType(type.ToLower()));
string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
document.Dispose();
return json;
}
private FormatType GetFormatType(string fileExtension)
{
if (string.IsNullOrEmpty(fileExtension))
throw new NotSupportedException("EJ2 DocumentEditor does not support this file format.");
switch (fileExtension)
{
case ".docx":
return FormatType.Docx;
case ".doc":
return FormatType.Doc;
case ".rtf":
return FormatType.Rtf;
case ".txt":
return FormatType.Txt;
// Add more cases for other supported file extensions if needed
default:
return FormatType.Docx; // Default to a specific format if the extension is not recognized
}
}
}