Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to handle a POSTed file to an ASP.NET Core application from Microsoft Flow

$
0
0

I have a Microsoft Flow setup with the following steps

  1. When a file is created (SharePoint) (this is pointing to a specific document library
  2. Http (Http), Method: Post, Uri (pointing to my app), Body: File Content from the SharePoint step above.

My app that I have written is hosted in an Azure Web App and is written in C# in ASP.NET Core.  I'm trying to convert the information that is coming into me from Flow back into the original document and I cannot figure out what is being sent.  When I look in Flow it says the body of the request that was sent is:

{"$content-type": "image/jpeg","$content": "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAQABADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD1C9EMuqzGK1juS+3P7rccgc4yMYxjv1q/ol0I4bfTpQVniXaoyDuQHjoTg7ccGsDU7O+0+xEdoJfMUKiKE84MB/dJ5B9mzj6VneFtO1271qx1G+hubaGBjmCSUfMSMZZQNoxzgDnPfGKqcnypEJW1R//Z"
}

In my application I originally tried to have a method that looks like 

[HttpPost]publicIActionResultProcess(List<IFormFile> files)

but that flow is not submitted a file, so I changed it to the below method

[HttpPost]publicIActionResultProcessJson([FromBody]FlowFile file){
...
}
publicclassFlowFile{[JsonProperty(PropertyName="$content-type")]publicstringContentType{ get;set;}[JsonProperty(PropertyName="$content")]publicstringContent{ get;set;}}

I also added a block of middleware, so that I could get the actual request body stream.  The middleware that I added has method that looks like this

public async TaskInvoke(HttpContext context){var requestBodyStream =newMemoryStream();var originalRequestBody = context.Request.Body;

            await context.Request.Body.CopyToAsync(requestBodyStream);
            requestBodyStream.Seek(0,SeekOrigin.Begin);var requestBodyText =newStreamReader(requestBodyStream).ReadToEnd();var bytes =StringToByteArray(requestBodyText);LogRequestMiddleware.LastBody=System.Text.Encoding.Default.GetString(bytes);

            requestBodyStream.Seek(0,SeekOrigin.Begin);
            context.Request.Body= requestBodyStream;

            await next(context);
            context.Request.Body= originalRequestBody;}privatestaticbyte[]StringToByteArray(string hex){returnEnumerable.Range(0, hex.Length)//.Where(x => x % 2 == 0).Select(x =>Convert.ToByte(hex.Substring(x,1),8)).ToArray();}

When I do this The response that I get is below and I cannot figure how to get it back to the correct string that I want.

&#xD;&#xA;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#x0;&#x10;JFIF&#x0;&#x1;&#x1;&#x1;&#x0;`&#x0;`&#x0;&#x0;&#xFFFD;&#xFFFD;&#x0;C&#x0;&#x8;&#x6;&#x6;&#x7;&#x6;&#x5;&#x8;&#x7;&#x7;&#x7;&#x9;&#x9;&#x8;&#xA;&#xC;&#x14;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