如何在C#中读取图像和文本值?

时间:2017-11-18 03:59:26

标签: c# function azure post

enter image description here

如您所见,我正在尝试通过POST命令向本地函数发送图像和名称。

如何在C#中读取这两个参数?

这是我尝试过但它只能读取文件图像。

   [FunctionName("Test")]
public static async Task<HttpResponseMessage> 
     Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = 
 null)]HttpRequestMessage req, TraceWriter log)
{
    //Check if the request contains multipart/form-data.
    if (!req.Content.IsMimeMultipartContent())
    {
        return req.CreateResponse(HttpStatusCode.UnsupportedMediaType);
    }


  foreach (var stream in contents.Contents)
    {


        try
        {
            var fileBytes = await stream.ReadAsByteArrayAsync();
            var fileinfo = new FileInfo(stream.Headers.ContentDisposition.FileName.Trim('"'));



         //Can Read File image like this.


        }
        catch(Exception e)
        {
            return req.CreateErrorResponse(HttpStatusCode.Conflict, e);
        }

2 个答案:

答案 0 :(得分:1)

您可以使用FileData

  1. 使用FormData属性获取已发布的文件
  2. 使用Pos属性获取基于密钥发布的任何表单数据的值。
  3. 选中File Upload and Multipart MIME

答案 1 :(得分:0)

  

有没有像使用内存流一样的解决方法?

根据您的要求,我认为您可以使用HttpContentMultipartExtensions.ReadAsMultipartAsync并获得vm.dataUpdate = angular.copy(vm.leaveList) ,然后您可以利用以下代码来阅读您上传的文件:

MultipartMemoryStreamProvider

此外,您可以按照issue关于基于var multipartMemoryStreamProvider= await req.Content.ReadAsMultipartAsync(); foreach (HttpContent content in multipartMemoryStreamProvider.Contents) { // for reading the uploaded file var filename= content.Headers.ContentDisposition.FileName.Trim('"'); var stream=await content.ReadAsStreamAsync(); //for formdata, you could check whether `content.Headers.ContentDisposition.FileName` is empty log.Info($"name={content.Headers.ContentDisposition.Name},value={await content.ReadAsStringAsync()}"); } 创建自定义MultipartFormDataMemoryStreamProvider。这个issue关于基于MultipartMemoryStreamProvider构建自定义InMemoryMultipartFormDataStreamProvider。