代码之家  ›  专栏  ›  技术社区  ›  RandomUs1r

如何在OWIN管道上的web api 2中接收多部分/表单数据

  •  0
  • RandomUs1r  · 技术社区  · 7 年前

    我是这样开始通话的:

    Upload.upload({
                    url: config.ApiUrl + 'Orders/CreateOrder',
                    data: request
                }).then(function (response) {
                    console.log('Success ' + response.config.data.file.name + 'uploaded. Response: ' + response.data);
                }, function (error) {
                    $scope.errors = "Oops! Something went wrong... " + error.statusText;
                });
    

    -----------------------------9623122368016
    Content-Disposition: form-data; name="somefield"
    
    Some Value
    -----------------------------9623122368016
    Content-Disposition: form-data; name="errorMessage"
    
    null
    -----------------------------9623122368016
    Content-Disposition: form-data; name="success"
    
    false
    -----------------------------9623122368016
    Content-Disposition: form-data; name="file"; filename="imapdf.pdf"
    Content-Type: application/pdf
    
    %PDF-1.2
    1 0 obj 
    <<
    /Producer()
    /Author()
    /Title()
    /Subject()
    /Keywords()
    /CreationDate(D:20040106)
    /ModDate(D:20040106)
    /Creator()
    >> 
    endobj
    2 0 obj 
    <<
    /Type/XObject
    /Subtype/Image
    /Name/wpt1
    /Width 41
    /Height 15
    /BitsPerComponent 8
    /ColorSpace/DeviceRGB
    /Length 100
    /Filter [/FlateDecode] >>
    stream
    xsqH0s 
    

    我无法访问 HttpContext.Current.Request.Files 因为我正在使用OWIN管道,在 Request.Content.ReadAsMultipartAsync

    我如何接收我的文件&它是web api 2中的关联表单数据?

    C代码更新:

    public Task<OrderRequest> CreateOrder(OrderRequest orderRequest)
    {
        try
        {
            var asdf = OwinHttpRequestMessageExtensions.GetOwinContext(Request);
            if (!Request.Content.IsMimeMultipartContent("multipart/form-data"))
            {
                var provider = new MultipartMemoryStreamProvider();
                this.Request.Content.ReadAsMultipartAsync(provider);
    
                var content = provider.Contents.First();
                var buffer = content.ReadAsByteArrayAsync();
            }
    
            var test = orderRequest;
            var a = HttpContext.Current.Request.Files;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    
        return Task.FromResult<OrderRequest>(null);
    }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Kirk Larkin    7 年前

    顾名思义, ReadAsMultipartAsync 是一个 作用因为你没有在等电话,所以你很可能会遇到这样的情况:你试图访问 Contents 在填充之前。

    要解决此问题,您可以更新 CreateOrder

    public async Task<OrderRequest> CreateOrder(OrderRequest orderRequest)
    {
        ...
        await this.Request.Content.ReadAsMultipartAsync(provider);
        ...
    }
    

    这里所改变的只是增加了 async 之前 Task<OrderRequest> await 之前 this.Request.Content.ReadAsMultipartAsync(provider); . 您还需要添加 等候 之前 content.ReadAsByteArrayAsync(); 出于同样的原因,然后你可以 return null Task.FromResult 最后。

    事实上,以下是这些更改的全部代码:

    public async Task<OrderRequest> CreateOrder(OrderRequest orderRequest)
    {
        try
        {
            var asdf = OwinHttpRequestMessageExtensions.GetOwinContext(Request);
            if (!Request.Content.IsMimeMultipartContent("multipart/form-data"))
            {
                var provider = new MultipartMemoryStreamProvider();
                await this.Request.Content.ReadAsMultipartAsync(provider);
    
                var content = provider.Contents.First();
                var buffer = await content.ReadAsByteArrayAsync();
            }
    
            var test = orderRequest;
            var a = HttpContext.Current.Request.Files;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    
        return null;
    }