9

我有一个 ASP.NET 应用程序,它提供了一个简单的表单来上传文件(图像)。看起来像这样:

public IActionResult Process()
{
    return View();
}

[HttpPost]
public IActionResult Process(List<IFormFile> files)
{
    var telemetry = new TelemetryClient();
    try
    {
        var result = files.Count + " file(s) processed " + Environment.NewLine;
        foreach (var file in files)
        {
            result += file.FileName + Environment.NewLine;
            var memoryStream = new MemoryStream();
            file.CopyTo(memoryStream);
            memoryStream.Seek(0, SeekOrigin.Begin);
            var binaryReader = new BinaryReader(memoryStream);
            var bytes = binaryReader.ReadBytes((int)memoryStream.Length);

            var imageInformation = ImageService.ProcessImage(bytes);

            ImageService.SaveImage(imageInformation.Result, bytes, file.FileName.Substring(file.FileName.LastIndexOf(".", StringComparison.Ordinal) + 1));
        }

        return View((object)result);
    }
    catch (Exception ex)
    {
        telemetry.TrackException(ex);
        throw;
    }
}

应用程序中的此表单工作正常。问题是我想使用 Microsoft Flow 将进入 SharePoint 库的文件提交到上面定义的 Web 应用程序。

我有文件流设置,它运行并且没有出错,但是当我查看 HTTP 操作结果的主体时,它说 0 个文件已处理,但什么也没做。

我设置的流程是

  1. 创建文件时 (SharePoint)(这是指向特定文档库
  2. Http (Http),方法:Post,Uri(指向我的应用程序),Body:来自上述 SharePoint 步骤的文件内容。

正如我提到的,这是发布到站点,但不能以 ASP.NET 方法可以处理的方式传递文件,因此它不处理任何内容。如何更改流程或 Post 方法,以便它可以工作。

更新了新信息 我用一个非常小的图像尝试了这个,所以我可以获得一些额外的请求信息。使用浏览器中的表单我尝试了这个并使用 Fiddler 得到以下请求原始结果:

POST https://os-gbsphotoretain.azurewebsites.net/Image/Process HTTP/1.1
Host: os-gbsphotoretain.azurewebsites.net
Connection: keep-alive
Content-Length: 924
Pragma: no-cache
Cache-Control: no-cache
Origin: https://os-gbsphotoretain.azurewebsites.net
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarySjQVgrsvAqJYXmST
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: https://os-gbsphotoretain.azurewebsites.net/Image/Process
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8
Cookie: _ga=GA1.3.955734319.1501514097; ai_user=UkqSf|2017-07-31T15:17:38.409Z; ARRAffinity=1628d46398b292eb2e3ba76b4b0f1eb1e30abd9bd1036d7a90b9c51f7baa2306; ai_session=/fPFh|1502738361594.15|1502738361594.15

------WebKitFormBoundarySjQVgrsvAqJYXmST
Content-Disposition: form-data; name="files"; filename="printer.jpg"
Content-Type: image/jpeg

     JFIF  ` `     C            



 $.' ",#(7),01444'9=82<.342   C         


2!!22222222222222222222222222222222222222222222222222     "                 

       } !1AQa "q2   #B  R  $3br    
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz                                                                                       

       w !1AQ aq"2 B        #3R br 
$4 % &'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz                                                                             ?  
    +X K     21 c Z  ] ӥg v  ; :          P     I f >   m;] ֬u nm   ` Q 1 P6 s 9 |b r|   G  
------WebKitFormBoundarySjQVgrsvAqJYXmST--

通过流程执行相同的图像,我得到以下作为流程中的主体:

{
  "$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"
}

所以看起来流是作为 JSON 提交的。我现在将尝试一些额外的处理作为测试,但如果有人知道我可以在 Web 应用程序中放置什么来处理这个问题,我将不胜感激。

我添加了一个新方法,如下所示,当我在本地运行它并传入 Flow 所说的正文时,该方法有效。但是当我从流中运行它时,我在 DeserializeObject 行中得到 value cannot be null 错误。如何获取 Flow 传入的信息。

[HttpPost]
    public IActionResult ProcessJson(string json)
    {
        var telemetry = new TelemetryClient();
        try
        {
            var result = "JSON processed " + Environment.NewLine;
            var details = (dynamic)Newtonsoft.Json.JsonConvert.DeserializeObject(json);
            var content = (string) details["$content"];
            var bytes = Convert.FromBase64String(content);

            ProcessBytes(bytes, "jpeg");
            return View("Process", result);
        }
        catch (Exception ex)
        {
            telemetry.TrackException(ex);
            throw;
        }
    }

我也尝试了一个带有这个签名的方法,但是没有运气,它也以 null 的形式出现

        [HttpPost]
        public IActionResult ProcessJson([FromBody]FlowFile file)
        {
...
        }



 public class FlowFile
    {

        [JsonProperty(PropertyName = "$content-type")]
        public string ContentType { get; set; }
        [JsonProperty(PropertyName = "$content")]
        public string Content { get; set; }
    }

我添加了一些中间件,这样我就可以获得原始的 Request.Body 并且由此产生的最终结果就是这个。我不确定这相当于什么。

&#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;&#xD;&#xC;&#xB;&#xB;&#xC;&#x19;&#x12;&#x13;&#xF;&#x14;&#x1D;&#x1A;&#x1F;&#x1E;&#x1D;&#x1A;&#x1C;&#x1C; $.&#x27; &quot;,#&#x1C;&#x1C;(7),01444&#x1F;&#x27;9=82&lt;.342&#xFFFD;&#xFFFD;&#x0;C&#x1;&#x9;&#x9;&#x9;&#xC;&#xB;&#xC;&#x18;&#xD;&#xD;&#x18;2!&#x1C;!22222222222222222222222222222222222222222222222222&#xFFFD;&#xFFFD;&#x0;&#x11;&#x8;&#x0;&#x10;&#x0;&#x10;&#x3;&#x1;&quot;&#x0;&#x2;&#x11;&#x1;&#x3;&#x11;&#x1;&#xFFFD;&#xFFFD;&#x0;&#x1F;&#x0;&#x0;&#x1;&#x5;&#x1;&#x1;&#x1;&#x1;&#x1;&#x1;&#x0;&#x0;&#x0;&#x0;&#x0;&#x0;&#x0;&#x0;&#x1;&#x2;&#x3;&#x4;&#x5;&#x6;&#x7;&#x8;&#x9;&#xA;&#xB;&#xFFFD;&#xFFFD;&#x0;&#xFFFD;&#x10;&#x0;&#x2;&#x1;&#x3;&#x3;&#x2;&#x4;&#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C;&#xFFFD;&#xFFFD;&#xFFFD;&#x2B;X&#xFFFD;K&#xFFFD;&#xFFFD;&#xFFFD;&#x1C;&#xFFFD;&#xFFFD;21&#xFFFD;c&#xFFFD;Z&#xFFFD;&#xFFFD;]&#x8;&#xFFFD;&#x4E5;&#x5;g&#xFFFD;v&#xFFFD;&#xFFFD;;&#xFFFD;&#x1E;:&#x13;&#xFFFD;&#xFFFD;&#x1C;&#x1A;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#x11;&#x1D;&#xFFFD;&#xFFFD;&#xFFFD;P&#xFFFD;&#xFFFD;&#x13;&#xFFFD;&#xC;&#x7;&#xFFFD;I&#xFFFD;&#x1F;f&#xFFFD;&gt;&#xFFFD;&#xFFFD;&#xFFFD;m;]&#xFFFD;&#x5AC;u&#x1B;&#xFFFD;nm&#xFFFD;&#xFFFD;&#xFFFD;`&#xFFFD;Q&#xFFFD;&#x12;1&#xFFFD;P6&#xFFFD;s&#xFFFD;9&#xFFFD;|b&#xFFFD;r|&#xFFFD;&#x10;&#xFFFD;&#xFFFD;G&#xFFFD;
4

3 回答 3

1

嗯,我有点不清楚文件是如何发送的:它是一个 json 对象,文件之前被转换为 base64 字符串还是文件内容?(html标题是指标)

如果你有 json 理论,你可以这样做:

var parsedFileContent = Newtonsoft.Json.JsonConvert.DeserializeObject<FlowFile>(json);

代替

var details = (dynamic)Newtonsoft.Json.JsonConvert.DeserializeObject(json);

它应该起作用,当且仅当;),您发布的内容是正确的

{
  "$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"
}

只需确保 json 在单行字符串中(确保没有像 \n 或类似的隐藏字符)

另一方面,在您的 Fiddler 捕获中,您有:

内容类型:multipart/form-data;

所以使用IFormFile的正确方法

所以提供的信息有点误导。您可以尝试传递更多的错误日志吗?“对象引用未发送到对象错误的实例”是非常普遍的,通常这些类型的错误会通过堆栈跟踪缩小范围。

于 2017-08-18T07:49:21.777 回答
0

我终于得到了这份工作。我需要做的是直接读取原始请求流,该流只是图像。Flow 所说的关于以 Base64 编码的 JSON 字符串提交图像的所有内容都是不正确的。我无法将它绑定到任何参数或作为 Request.Form.Files,但我可以直接读取流并直接从中保存图像。

于 2017-08-25T13:11:50.617 回答
0

而不是拥有List<IFormFile>参数,而是访问 中的Files属性HttpContext.Request

于 2017-08-16T16:29:30.030 回答