locked
How do I upload a file stream to the web API 2 in ASP.NET via JSON object? RRS feed

  • Question

  • 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
    }




    • Moved by litdev Sunday, July 24, 2016 11:37 AM
    Monday, July 11, 2016 11:55 AM

Answers

  • I would recommend to link to this website it's all about AngularJS

    Introduction to AngularJS

    • Proposed as answer by Just Karl Thursday, July 28, 2016 2:31 PM
    • Marked as answer by Dave PatrickMVP Monday, August 1, 2016 2:27 AM
    Tuesday, July 12, 2016 12:36 AM
  • Hello,

    This should be asked in Microsoft's ASP.Net forums:

    http://forums.asp.net/

    As Microsoft's ASP.Net forums are on a different platform, we cannot move the question for you.

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog: Unlock PowerShell
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join('6D73646E5F6B61726C406F75746C6F6F6B2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})

    Thursday, July 28, 2016 2:31 PM

All replies

  • This forum is about the Small Basic programming language ! Your question can better be placed in another forum.

    Jan [ WhTurner ] The Netherlands

    Monday, July 11, 2016 2:07 PM
  • I would recommend to link to this website it's all about AngularJS

    Introduction to AngularJS

    • Proposed as answer by Just Karl Thursday, July 28, 2016 2:31 PM
    • Marked as answer by Dave PatrickMVP Monday, August 1, 2016 2:27 AM
    Tuesday, July 12, 2016 12:36 AM
  • Hello,

    This should be asked in Microsoft's ASP.Net forums:

    http://forums.asp.net/

    As Microsoft's ASP.Net forums are on a different platform, we cannot move the question for you.

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog: Unlock PowerShell
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join('6D73646E5F6B61726C406F75746C6F6F6B2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})

    Thursday, July 28, 2016 2:31 PM