Need to remove the following part of code
using (StreamReader fileStream = new StreamReader(FileUpload1.PostedFile.InputStream))
{
fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
fileStream.Close();
}
and replace
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileBytes, 0, fileBytes.Length);
requestStream.Close();
}
with
using (var requestStream = request.GetRequestStream())
{
CopyStream(FileUpload1.PostedFile.InputStream, requestStream);
// no need to requestStream.Close(); - using does that for you
}
CopyStream Method
public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[32768];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
The uploaded file will not get corrupted