0

I have a scenario in which i want to upload the file stream from client side (I am using AngularJS) to the Web Api in asp .net via JSON object. So there will be some fields in JSON object in which one field is file stream.How do i create a JSON object and send it to the web api which get model bind at api side to my model.

    $scope.fileUploadData = {
        TextId: 304765,
        DocumentId: 0,
        PatientId: 166158,
        file://This is my file stream property in JSON
}

    public ApiActionResult CreateDocument(DocumentVM model)
{
  //Some logic here.....
}

// DocumentVM class will be as follows
public class DocumentVM
{
        long TextId;
        long DocumentId;
        long PatientId;
        Stream file;
}

Please provide me the sample code snippets how to achieve it.
What I have tried: I googled lots but i found the solutions like using multipart form data. In which JSON and file stream appended and then send it to the web api. I used following logic to extract the multi part form data.

    public async Task<ApiActionResult> CreateDocument()
{
  MultipartFormDataStreamProvider provider = 
            new MultipartFormDataStreamProvider(Path.GetTempPath());
            var result = await Request.Content.ReadAsMultipartAsync(provider);
            var model= result.FormData["Metadata"];
            var fileData = result.FileData;//How it will retriev the file data
}
2
  • With simple json object you can not stream file. You need to create multi-part request. At javascript side, you need to create File object, which will be passed to server using multi-part request. Commented Jul 4, 2016 at 9:27
  • So there is no option other than multi part form data,to send stream to the API? Commented Jul 12, 2016 at 12:36

1 Answer 1

1

Don't know whether you got a solution or not for your problem...but I had the same scenario and I solved it like below

There are two ways of doing the same

Approach 1: Use MultipartFormData to pass your stream directly to your WebApi,check this link

In your question you have mentioned that you tried this approach but not getting any day..my guess is you have not used enctype="multipart/form-data" in your html form.

Approach 2: You can create a custom model binder which then can be decorated on you action's model like below

public class MyModelBinder : IModelBinder
    {
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            // Extract the posted data from the request
            // Convert the base64string to Stream
            // Extract the model from the bindingcontext
            // Assign the model's property with their values accordingly   
        }
    }

In you controller

public ApiActionResult SaveDocument([ModelBinder(typeof(MyModelBinder))]DocumentVM contentDTO)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.