Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

c# - Parsing multipart form data on .NET

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.
question from:https://stackoverflow.com/questions/65887925/parsing-multipart-form-data-on-net

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...