I have an uploaded CSV file that is posted to a Web API route. I have been able to get the CSV into memory via a byte array and that is where I am stuck. I want to take each row of the CSV, add three additional fields that are posted from the upload form along with the file, and insert every CSV row with additional fields to a SQL server table. How do I take my byte array, transform the rows, and insert the data?
Here is my AngularJS that uses ng-file-upload.
$scope.upload = function (file) {
Upload.upload({
url: 'api/UploadProfile',
data: {
file: file,
'ProfileName': $scope.ProfileName,
'SubmittedBy': $scope.SubmittedBy,
'InsertDate': $scope.InsertDate
}
})}
And here is my Web API controller
public class ProfileUploadController : ApiController
{
private BIMarketOrderEntities db = new BIMarketOrderEntities();
[HttpPost, Route("api/UploadProfile")]
public async Task<IHttpActionResult> Upload()
{
if (!Request.Content.IsMimeMultipartContent())
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var file in provider.Contents)
{
var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
var buffer = await file.ReadAsByteArrayAsync();
// How do I get 'buffer' to my database while adding the additional fields?
}
return Ok();
}
}