Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello All,
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.

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

C#
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.
C#
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
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900