0

I am running an Angular JS application on top of a C#/.NET legacy framework.

I need to upload & save a file along with some POST data variables, and therefore using the FormData api.

let formData = new FormData();
formData.append('co-lo-import', file);
formData.append('data', someString);

Then I make an xhr request like so:

$http.post("//some/dot-net/endpoint/Default.aspx",
           formData,
           { headers: { "Content-Type" : "multipart/form-data" } 
});

On the .NET side, I'm trying to grab these values from the Request variable.

string someString = Request.Form['data'];

HttpFileCollection uploadedFiles = null;

if(Request.Files != null){
    uploadedFiles = Request.Files;
}

But these values are always empty. There is never anything in Request.Form

I tried Request.Form.Get("data") which comes up empty also.

I am seeing the file and data in the request payload:

request body

How can I grab this data on the back-end using .NET?

yevg
  • 1,846
  • 9
  • 34
  • 70
  • 1
    When posting objects created by the [FormData API](https://developer.mozilla.org/en-US/docs/Web/API/FormData), it is important to set the Content-type header to `undefined`. – georgeawg Feb 22 '19 at 06:24
  • @georgeawg this was the issue. Thank you – yevg Feb 22 '19 at 21:11

1 Answers1

0

If the data is not available in Request.Form then try to access the bytes from raw input stream from HTTP request:

using(var reader = new StreamReader(Request.InputStream))
{
    content = reader.ReadToEnd();
}
FaizanHussainRabbani
  • 3,256
  • 3
  • 26
  • 46