如何在C#中读取图像和文本值? [英] How to read an image and text value in C#?

查看:198
本文介绍了如何在C#中读取图像和文本值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



正如您所看到的,我正尝试通过POST命令发送图像和名称到本地功能。



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

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

$ p $ [FunctionName(Test)]
public static async Task< HttpResponseMessage>
Run([HttpTrigger(AuthorizationLevel.Anonymous,get,post,Route =
null)] HttpRequestMessage req,TraceWriter log)
{
//检查请求包含多部分/表单数据。
if(!req.Content.IsMimeMultipartContent())
{
return req.CreateResponse(HttpStatusCode.UnsupportedMediaType);



foreach(contents.Contents中的var流)
{


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



//可以读取像这样的文件图像。



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


解决方案


按照你的要求,我认为你可以使用 b msdn.microsoft.com/en-us/library/hh944544(v=vs.118).aspxrel =nofollow noreferrer> HttpContentMultipartExtensions.ReadAsMultipartAsync ,您将获得 MultipartMemoryStreamProvider ,那么你可以利用下面的代码来读取你上传的文件:

  var multipartMemoryStreamProvider = await req。 Content.ReadAsMultipartAsync(); 
foreach(Htt在multipartMemoryStreamProvider.Contents中的pContent内容)
{
//用于读取上传的文件
var filename = content.Headers.ContentDisposition.FileName.Trim(''');
var stream =等待content.ReadAsStreamAsync();

//对于formdata,你可以检查'content.Headers.ContentDisposition.FileName`是否为空
log.Info($name = {content.Headers.ContentDisposition.Name}, value = {await content.ReadAsStringAsync()});
}

此外,您可以按照 issue ,基于 MultipartMemoryStreamProvider创建您的自定义MultipartFormDataMemoryStreamProvider 。关于建立自定义的问题 InMemoryMultipartFormDataStreamProvider基于 MultipartStreamProvider


As you can see, I am trying to send an image, and a name through a POST command to a local function.

How Can I read both of these parameters in C#?

This is what I have tried but It can only read File Image.

   [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);
        }

解决方案

Is there a workaround to do this like using memory stream?

According to your requirement, I assumed that you could use HttpContentMultipartExtensions.ReadAsMultipartAsync and you would get the MultipartMemoryStreamProvider, then you could leverage the following code for reading your uploaded files:

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()}");
}

Moreover, you could follow this issue about creating your custom MultipartFormDataMemoryStreamProvider based on MultipartMemoryStreamProvider. And this issue about building custom InMemoryMultipartFormDataStreamProvider based on MultipartStreamProvider.

这篇关于如何在C#中读取图像和文本值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆