I'm doing the below to upload a .pptx file and save it on the disk of the server.
When I try to open the file, it says that it's corrupt, and cannot be opened.
Is this because of my encoding type or something different?
Is it possible to send any arbitrary file as binary in an POST and have it reach the server in one piece?
public static void ListenerCallback(IAsyncResult result)
{
HttpListener listener = (HttpListener)result.AsyncState;
HttpListenerContext context = listener.EndGetContext(result);
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
StreamReader reader = new StreamReader(request.InputStream);
var res = reader.ReadToEnd();
reader.Close();
toLog.Add(res);
NameValueCollection coll = HttpUtility.ParseQueryString(res);
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(res);
File.WriteAllBytes("output.pptx", bytes);
response.StatusCode = 200;
response.ContentType = "text/html";
using (StreamWriter writer = new StreamWriter(context.Response.OutputStream, Encoding.UTF8))
writer.WriteLine("File Uploaded");
response.Close();
}
This is the Postman preview of what I'm sending:
POST HTTP/1.1
Host: localhost:80
Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation
Cache-Control: no-cache
Postman-Token: 9a04aa33-cd44-d238-6873-b330524f4ae5
undefined
If I open up the source (non-corrupt) file in np++, I see that the editor chooses to encode it in ANSI.
I'm currently encoding it in UTF8, and I don't appear to have the option to encode in ANSI.
Also, when comparing the file size, I lose a kilobyte of data.
This writes an empty file to the disk.
public static void ListenerCallback(IAsyncResult result)
{
HttpListener listener = (HttpListener)result.AsyncState;
HttpListenerContext context = listener.EndGetContext(result);
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
StreamReader reader = new StreamReader(request.InputStream);
var res = reader.ReadToEnd();
reader.Close();
Console.WriteLine(res);
NameValueCollection coll = HttpUtility.ParseQueryString(res);
using (var outp = File.OpenWrite("output.pptx"))
{
request.InputStream.CopyTo(outp);
}
response.StatusCode = 200;
response.ContentType = "text/html";
using (StreamWriter writer = new StreamWriter(context.Response.OutputStream, Encoding.UTF7))
writer.WriteLine("File Uploaded");
response.Close();
}
I don't have time to test this right now, but try this:
public static void ListenerCallback(IAsyncResult result)
{
HttpListener listener = (HttpListener)result.AsyncState;
HttpListenerContext context = listener.EndGetContext(result);
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
using (var out = File.OpenWrite("output.pptx"))
{
request.InputStream.CopyTo(out);
}
response.StatusCode = 200;
response.ContentType = "text/html";
using (StreamWriter writer = new StreamWriter(context.Response.OutputStream, Encoding.UTF8))
writer.WriteLine("File Uploaded");
response.Close();
}