'Project is to upload excel file and that excel file needs to get updated to Database. when i run i getting an error in swagger
I tried code where it can able to read and upload excel data to the database. but when i run i getting error. please someone can fix this issue, can you able to help me out.I tried code where it can able to read and upload excel data to the database. but when i run i getting error. please someone can fix this issue, can you able to help me out.I tried code where it can able to read and upload excel data to the database. but when i run i getting error. please someone can fix this issue, can you able to help me out.I tried code where it can able to read and upload excel data to the database. but when i run i getting error. please someone can fix this issue, can you able to help me out.I tried code where it can able to read and upload excel data to the database. but when i run i getting error. please someone can fix this issue, can you able to help me out.I tried code where it can able to read and upload excel data to the database. but when i run i getting error. please someone can fix this issue, can you able to help me out.I tried code where it can able to read and upload excel data to the database. but when i run i getting error. please someone can fix this issue, can you able to help me out.I tried code where it can able to read and upload excel data to the database. but when i run i getting error. please someone can fix this issue, can you able to help me out.I tried code where it can able to read and upload excel data to the database. but when i run i getting error. please someone can fix this issue, can you able to help me out
namespace ExcelUpload.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PersonScoreController : ControllerBase
{
private readonly ExamroomV2UATContext _context;
private readonly IWebHostEnvironment _environment;
private readonly PersonEventScore PersonEventScores;
public PersonScoreController(ExamroomV2UATContext context,IWebHostEnvironment environment)
{
_context = context;
_environment = environment;
}
// POST api/<PersonScoreController>
[HttpPost]
[Route("Upload")]
public async Task<ActionResult>Upload(List<IFormFile> files)
{
long size = files.Sum(f => f.Length);
var rootPath = Path.Combine(_environment.ContentRootPath, "Resources", "PersonEventScore");
if(!Directory.Exists(rootPath))
Directory.CreateDirectory(rootPath);
foreach(var file in files)
{
var filepath = Path.Combine(rootPath, file.FileName);
using (var stream =new FileStream(filepath,FileMode.Create))
{
var ViewPersonEvent = new ViewPersonEvent
{
FirstName = file.FileName,
LastName = file.FileName,
EmailId = file.FileName,
ExamMode = file.FileName,
};
await file.CopyToAsync(stream);
_context.PersonEventScores.Add(PersonEventScores);
await _context.SaveChangesAsync();
}
}
return Ok(new {count=files.Count,size});
}
}
}**
Solution 1:[1]
First, it is better to tell use the detail error, so that it is easier for us to understand the issue and help you fix the problem.
Second, I checked the Upload method, since you don't set the value for the PersonEventScores, it will be null when inserted into the database, so the error might relate it, right? You can check it.
You can modify the code as below (I just create a PersonEventScores to store the list of ViewPersonEvent, you can change it based on your real requirement):
[HttpPost]
[Route("Upload")]
public async Task<ActionResult> Upload(List<IFormFile> files)
{
long size = files.Sum(f => f.Length);
var rootPath = Path.Combine(_environment.ContentRootPath, "Resources", "PersonEventScore");
if (!Directory.Exists(rootPath))
Directory.CreateDirectory(rootPath);
//define a list to store the ViewPersionEvent.
List<APIWebApp.Models.ViewPersonEvent> PersonEventScores = new List<Models.ViewPersonEvent>();
foreach (var file in files)
{
var filepath = Path.Combine(rootPath, file.FileName);
using (var stream = new FileStream(filepath, FileMode.Create))
{
var ViewPersonEvent = new APIWebApp.Models.ViewPersonEvent()
{
FirstName = file.FileName,
LastName = file.FileName,
EmailId = file.FileName,
ExamMode = file.FileName,
};
//add the ViewPersonEvent to the list.
PersonEventScores.Add(ViewPersonEvent);
await file.CopyToAsync(stream);
}
}
// to insert multiple data into the database. we can use the AddRange method
//_context.PersonEventScores.AddRange(PersonEventScores);
//await _context.SaveChangesAsync();
return Ok(new { count = files.Count, size });
}
Then, the result like this:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zhi Lv |

