2

I have a simple web server using HttpListener and I just added some HTML that lets the user upload a file, along the lines of this:

<form action="upload.php" method="post" enctype="multipart/form-data">
  Select image to upload:
  <input type="file" name="fileToUpload" id="fileToUpload">
  <input type="submit" value="Upload Image" name="submit">
</form>

My server receives an HTTP request with the method POST as expected and the input stream contains something of this form:

------WebKitFormBoundaryKmI6YLDrfViLaOWc
Content-Disposition: form-data; name="filename"; filename="CableHook.scad"
Content-Type: application/octet-stream

$fn=90;
...
}

------WebKitFormBoundaryKmI6YLDrfViLaOWc
Content-Disposition: form-data; name="submit"

Upload
------WebKitFormBoundaryKmI6YLDrfViLaOWc--

How do I parse the multipart form data on .NET to recover the file that the client uploaded to the server?

I have tried this:

use content = StreamContent(request.InputStream)
content.Headers.ContentType <- Headers.MediaTypeHeaderValue.Parse "multipart/form-data"
let provider = content.ReadAsMultipartAsync().Result

but it dies with the error:

Invalid 'HttpContent' instance provided. It does not have a 'multipart'
content-type header with a 'boundary' parameter.
J D
  • 48,105
  • 13
  • 171
  • 274
  • This is the answer https://stackoverflow.com/questions/8466703/httplistener-and-file-upload – derodevil Jan 25 '21 at 16:47
  • Why are you assigning something to content.Headers.ContentType? Won't the header value that was submitted be in the input stream? In any event, you're just assigning the string "multipart/form-data", which is incomplete (as the error message says). – Jim Foye Jan 26 '21 at 15:09

0 Answers0