I am using Valum's AJAX File Uploader to upload the csv file. The code to initialize it is :
$(document).ready(function () {
var uploader = new qq.FileUploader({
element: document.getElementById('file-uploader'),
// path to server-side upload script
action: 'handlers/PhoneNumberListHandler.ashx',
buttonTitle: 'Upload Document',
buttonText: ' Upload',
allowedExtensions: ['csv'],
sizeLimit: 1024000, // max size
});
});
<div id="file-uploader"></div>
In c# handler class I have written the following code so far:
HttpRequest request = context.Request;
byte[] buffer = new byte[request.ContentLength];
Stream inputStream;
string fileName;
inputStream = request.InputStream;
fileName = request["qqfile"];
using (var reader = new StreamReader(inputStream))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';'); // gives some abstract characters
}
}
My csv file have three columns Phone, Firstname, Lastname where first column is compulsory and rest are optional.
I am now stuck as I am not able to read the .csv file. Can someone please help me and suggest how can I read a csv file content from the stream or is there any other way to do it? I want to extract the csv content and save each line separately in database table.
Update: I made changes in saving the csv file and now the content look like this:
PhoneNumber,FirstName,LastName
11111,Taj,Mahal
22222,Oberai,
33333,,Lake Palace
44444,,
Modified the above code:
using (var reader = new StreamReader(inputStream))
{
List<string> listA = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';');
listA.Add(values[0]);
}
}
I want to get the individual value of the columns and here I am getting a full line in one string. To get individual value I have to further split it with "," and get the values. I want to know is there a better way to do this?