JavaScript readAsArrayBuffer to bytes and pass it to MVC controller

AzureDevUser 1 Reputation point
2021-10-03T05:33:43.587+00:00

Hello all I have the following code where I have created a model to pass the documents. My model is as follows

public class Student
{
    public string StudentName {get;set;}
    public List<Document> Documents { get; set; }
}

public class Document 
{
  public string DocumnetName {get;set;}
   public byte[] DocumentData {get;set;}
}

public JsonResult PostUserData(Student StudentData)
{

}

Here is the fiddle I am trying out https://jsfiddle.net/DorababuMeka/7g9yLrs3/23/

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,411 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,266 Reputation points
    2021-10-03T15:15:30.117+00:00

    if your client code is converting to base64, then its passed as string, so you need a view model. once received on the server, it can convert the base64 string back to a buffer.


  2. Yijing Sun-MSFT 7,071 Reputation points
    2021-10-04T08:03:42.907+00:00

    Hi @AzureDevUser ,
    As far as I think,you could convert to base64 string using the javascript.And then you could use ajax to post the parameter to the controller.Just like this:
    137300-new-text-document-2.txt
    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. AgaveJoe 27,656 Reputation points
    2021-10-04T13:00:17.157+00:00

    File upload is a very straight forward in MVC using an HTML Form and the file input element. The browser handles file stream so there is little reason to convert the file to an array manually.

    ASP.NET MVC 5 with EF 6 - Working With Files

    If you are building a JavaScript solution then take advantage of the FormData object to upload a file.

    Using FormData Objects
    Using files from web applications

    If you still need assistance then share your code so the community can see how your are approaching this problem.