How to save a file(s) to the documents directory from HttpListenerRequest.InputStream

Hi,

I have a webpage that I display on a PC via a web server from my device over http://192.168.x.xxx:8080 to choose files.

<form method="post" enctype="multipart/form-data"> <p>Choose files:<br> <input name="Datei" type="file" size="50" multiple></p> <button>Upload to your mobile device</button></form>

After I pushed the button on the webpage my app calls the HttpListener like this.

`public void Start()
{
ThreadPool.QueueUserWorkItem((o) =>
{
try
{
while (_listener.IsListening)
{
ThreadPool.QueueUserWorkItem((c) =>
{
var ctx = c as HttpListenerContext;
try
{
if (ctx.Request.HttpMethod == "POST")
{
// TODO
// Save files from InputStream to the documents directory.
}

                            string rstr = _responderMethod(ctx.Request);
                            byte[] buf = Encoding.UTF8.GetBytes(rstr);
                            ctx.Response.ContentLength64 = buf.Length;
                            ctx.Response.OutputStream.Write(buf, 0, buf.Length);
                        }
                        catch (Exception ex) 
                        {
                            throw ex;
                        } // suppress any exceptions
                        finally
                        {
                            // always close the stream
                            ctx.Response.OutputStream.Close();
                        }
                    }, _listener.GetContext());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        });
    }`

So far so good, but how can I save the files from the HttpListenerRequest.InputStream to the documents directory?

Thanks

Arno

Sign In or Register to comment.