In this article I will explain with an example, how to save BYTE Array to audio file (MP3) in ASP.Net Core (.Net Core 8) Razor Pages.
Note: For beginners in ASP.Net Core (.Net Core 8) Razor Pages, please refer my article ASP.Net Core 8 Razor Pages: Hello World Tutorial with Sample Program example.
 
 

Audio File Location

The sample audio file (MP3) is located inside the Files Folder (Directory) of wwwroot Folder (Directory).
ASP.Net Core Razor Pages: Save Byte Array to MP3
 
 

Index PageModel (Code-Behind)

Inside the PageModel, first the private property IWebHostEnvironment interface is created.
Then, the interface is injected into the Constructor (IndexModel) using Dependency Injection method and the injected object is assigned to private property (created earlier).
The PageModel consists of following Handler methods.

Handler method for handling GET operation

This Handler method left empty as it is not required.
 

Handler method for handling POST operation

Inside this Handling method, first the path of the sample audio file (MP3) is determined using IWebHostEnvrionment interface.
Note: For more details about IWebHostEnvironment interface, please refer my article Using IWebHostEnvironment in ASP.Net Core. For more details on how to access Static Files in Core, please refer my article Static Files (Images, CSS and JS files) in ASP.Net Core.
 
Then, the BYTE Array of the sample audio file (MP3) is determined using ReadAllBytes method of the File class.
After that, the BYTE Array is saved to another audio file (MP3) using WriteAllBytes method of File class and the audio file (MP3) is created inside the Files Folder (Directory).
Finally, the success message is set into a TempData object.
public class IndexModel : PageModel
{
    private IWebHostEnvironment Environment;
    public IndexModel(IWebHostEnvironment _environment)
    {
        this.Environment = _environment;
    }
 
    public void OnGet()
    {
    }
 
    public void OnPostSave()
    {
        string path = Path.Combine(this.Environment.WebRootPath, "Files/");
 
        // Converting the file to Byte Array.
        byte[] bytes = System.IO.File.ReadAllBytes(path + "Sample.mp3");
 
        // Saving Byte Array to mp3.
        System.IO.File.WriteAllBytes(path + "Welcome.mp3", bytes);
 
        // Showing message.
        TempData["Message"] = "File have been saved.";
    }
}
 
 

Razor Page (HTML)

HTML Markup

The Razor Page consists of an HTML Form which consists of a Submit Button.
The Submit Button has been set with the POST Handler method using the asp-page-handler attribute.
Note: In the Razor PageModel, the Handler method name is OnPostSave but here it will be specified as Save when calling from the Razor HTML Page.
 

Submitting the Form

When the Submit Button is clicked, the TempData object is checked for NULL and if it is not NULL then the value of the TempData object is displayed using JavaScript Alert Message Box.
@page
@modelByte_Array_MP3_Core_Razor.Pages.IndexModel
@addTagHelper*,Microsoft.AspNetCore.Mvc.TagHelpers
@{
     Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post">
        <input id="btnSave" type="submit" value="Save" asp-page-handler="Save" />
    </form>
    @if (TempData["Message"] != null)
    {
        <script type="text/javascript">
            window.onload = function () {
                alert('@TempData["Message"]');
            };
        </script>
    }
</body>
</html>
 
 

Screenshot

ASP.Net Core Razor Pages: Save Byte Array to MP3
 
 

Downloads