如何使用默认的`run()`方法中的`MultipartHttpServletRequest`类? [英] How to use the `MultipartHttpServletRequest` class from the default `run()` method?
问题描述
public HttpResponseMessage run(
@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<optional<string>> request,
final ExecutionContext context) {</optional<string>
只有 HttpRequestMessage
run()
method。< br style ="">我需要声明和使用 Mu ltipartHttpServletRequest
从multipart / data请求中获取文件。
There's only HttpRequestMessage
parameter on the run()
method.
I need to declare and use MultipartHttpServletRequest
to fetch a file from the multipart/data request.
请给我一些建议。
推荐答案
我不认为可以使用
MultipartHttpServletRequest
因为
的函数执行方式。
I don't think it would be possible to use
MultipartHttpServletRequest
because of the way how the functions are executed.
但是你仍然可以提取 多部分
数据
来自 HttpRequest
。这是一个快速而又脏的样本供您开始使用
But you should still be able to extract multipart
data
from HttpRequest
. Here is a quick and dirty sample for you to get started with
[FunctionName("MultipartSample")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req,
ILogger log)
{
var data = await req.Content.ReadAsMultipartAsync();
foreach (var val in data.Contents)
{
using (var fileStream = File.Create("D:\\sample.pdf"))
{
(await val.ReadAsStreamAsync()).CopyTo(fileStream);
}
}
return (ActionResult)new OkObjectResult("Cool!");
}
这篇关于如何使用默认的`run()`方法中的`MultipartHttpServletRequest`类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!