I am trying to figure out how to send a file uploaded from the UI(angular) and send it to a .NET web service for it to parse the CSV file and create a list of objects.
My current idea of how the logic should work is
File upload ---> Web Service(to parse file) --> Web API ---> Database
is this right?
What am I missing to send this file to the service and then from the service to the API Controller?
HTML:
<input type="file" (change)="onFileSelected($event)">
<button type="submit" (click)="onUpload" class="button">Upload</button>
Web service:
static void Main(string[] args)
{
string currentDirectory = Directory.GetCurrentDirectory();
DirectoryInfo directory = new DirectoryInfo(currentDirectory);
var fileName = Path.Combine(directory.FullName, "sample-data.csv");
var fileContents = ReadMonitoredEvent(fileName);
}
public static string ReadFile(string fileName)
{
using (var reader = new StreamReader(fileName))
{
return reader.ReadToEnd();
}
}
public static List<MonitoredEvent> ReadMonitoredEvent(string fileName)
{
var monitoredEventResults = new List<MonitoredEvent>();
using (var reader = new StreamReader(fileName))
{
string line = "";
reader.ReadLine();
while ((line = reader.ReadLine()) != null)
{
var monitoredEvent = new MonitoredEvent();
string[] values = line.Split(',');
DateTime eventDate;
TimeSpan eventTime;
float tlc;
float totalOrder;
//String.Format("{Short Date}", DateTime.Now);
if (DateTime.TryParse(values[3], out eventDate))
{
var date = eventDate.Date;
monitoredEvent.EventDate = date;
}
if(TimeSpan.TryParse(values[4], out eventTime))
{
monitoredEvent.Time = eventTime;
}
if (float.TryParse(values[6], out tlc))
{
monitoredEvent.TLC = tlc;
}
if (float.TryParse(values[5], out totalOrder))
{
monitoredEvent.TotalOrder = totalOrder;
}
monitoredEvent.Venue = values[0];
monitoredEvent.Event = values[1];
monitoredEvent.Section = values[2];
monitoredEvent.TicketType = values[7];
monitoredEvent.Source = values[8];
monitoredEvent.TicketPage = values[9];
monitoredEventResults.Add(monitoredEvent);
}
}
return monitoredEventResults;
}
I don't have any code for the controller nor the typescript.
I want this file to be able to go from a user uploading it to all the objects being stored in the database.