I have a javascript code that uploads a file to the server using XMLHttpRequest object. I am accepting the file as a Stream in the web service that is written using C#:
int uploadFile(Stream file)
{
using (FileStream fileStream = File.Create(System.AppDomain.CurrentDomain.BaseDirectory + "file.csv"))
{
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = file.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, len);
}
}
//process file
return 0;
}
In javascript the file is sent as a form data object:
var fd = new FormData(document.forms.namedItem("file"));
var req = new XMLHttpRequest();
req.open("POST", url, true);
req.send(fd);
Now, I am trying to save the Excel file on the server. I am reading the stream into a byte array and writing it to a csv file. But the error I am facing is corrupted data in the csv file.
Where am I going wrong or is there any other way to upload and save file through web service?
Thanks.