Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post File as Parameter to Odata Action #895

Open
digitalpowers opened this issue Jan 25, 2017 · 2 comments
Open

Post File as Parameter to Odata Action #895

digitalpowers opened this issue Jan 25, 2017 · 2 comments
Labels

Comments

@digitalpowers
Copy link

digitalpowers commented Jan 25, 2017

Short summary (3-5 sentences) describing the issue.
I am trying to allow for the use of an action that actually accepts a file (from multipart/form-data presumably). I have tried using all the resources I can find on the internet that refer to how to add custom formatters but some of that code has changed in v6.0 and I cannot find any resources on how to allow a multipart/form-data to be read into an ODataActionParameter.

I see there are integration points that have been added via the IServiceProvider DI interface however I dont have a clue where I am supposed to inject my service provider that is backed by my DI container (Windsor consequently).

Here is a subset of the packages I am using to try to get there.

  <package id="IdentityServer3" version="2.5.4" targetFramework="net451" />
  <package id="Microsoft.AspNet.Cors" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.OData" version="6.0.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.Extensions.DependencyInjection" version="1.0.0" targetFramework="net451" />
  <package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="1.0.0" targetFramework="net451" />
  <package id="Microsoft.OData.Core" version="7.0.0" targetFramework="net451" />
  <package id="Microsoft.OData.Edm" version="7.0.0" targetFramework="net451" />
  <package id="Microsoft.Owin" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.Cors" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.Diagnostics" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.FileSystems" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.Host.HttpListener" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.Hosting" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.SelfHost" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Owin.StaticFiles" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.Spatial" version="7.0.0" targetFramework="net451" />
  <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net451" />
  <package id="Owin" version="1.0" targetFramework="net451" />

Assemblies affected

Which assemblies and versions are known to be affected e.g. OData .Net lib 6.15-beta.
Using System.Web.OData 6.0 from Nuget

I'm fairly willing to work around things as needed, I already have a custom IODataRoutingConvention for routing to my actions and while i was able to get an action to forward on to my controller if I had no ODataActionParameter on the parameter list, I would like to be able to keep having other parameters as well as a file if needed.

@developermj
Copy link

I too am curious of how files can be handled in odata. I currently have an endpoint that accepts a base64 encoded file as a parameter but it doesn't feel right. Is this question an ideal way of handling files in odata? Is there another preferred way?

@digitalpowers
Copy link
Author

digitalpowers commented Sep 11, 2018

I can tell you what I ended up doing

ODataActionParameters actionParameters = new ODataActionParameters();
MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(mFileUploadLocation);
Task<MultipartFormDataStreamProvider> multipartAsync = Request.Content.ReadAsMultipartAsync(provider);
MultipartFormDataStreamProvider multipart = multipartAsync.Result;
foreach(MultipartFileData fileData in multipart.FileData)
{
    actionParameters.Add(fileData.Headers.ContentDisposition.Name.Trim('"'), new FileArgument(fileData.LocalFileName));
}

Then the action method doesnt take the ODataActionParameter since you create your own parameters. It is not easy to accept other parameters with the file because it has to be form data encoded (rather than json) and you have to deserialize it more manually.

I think that is about right, i dont have the full source handy atm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants