In this article I will explain with an example, how to save BYTE Array to audio file (MP3) in
ASP.Net using C# and VB.Net.
Audio File Location
The sample audio file (MP3) is located inside the
Files Folder (Directory) of
ASP.Net project.
HTML Markup
The HTML Markup consists of following control:
Button – For saving BYTE Array to MP3.
The Button has been assigned with an OnClick event handler.
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="OnSave" />
Namespaces
You will need to import the following namespace.
C#
VB.Net
Saving Byte Array to MP3 using C# and VB.Net
When the Save Button is clicked, the BYTE Array of the sample audio file (MP3) is determined using ReadAllBytes method of the File class.
Then, 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).
C#
protected void OnSave(object sender, EventArgs e)
{
// Converting the file to Byte Array.
byte[] bytes = File.ReadAllBytes(Server.MapPath("~/Files/Sample.mp3"));
// Saving Byte Array to mp3.
File.WriteAllBytes(Server.MapPath("~/Files/Welcome.mp3"), bytes);
// Showing message.
ClientScript.RegisterClientScriptBlock(this.GetType(), "message", "alert('File have been saved.')", true);
}
VB.Net
Protected Sub OnSave(sender As Object, e As EventArgs)
' Converting the file to Byte Array.
Dim bytes As Byte() = File.ReadAllBytes(Server.MapPath("~/Files/Sample.mp3"))
' Saving Byte Array to mp3.
File.WriteAllBytes(Server.MapPath("~/Files/Welcome.mp3"), bytes)
' Showing Message.
ClientScript.RegisterClientScriptBlock(Me.GetType(), "message", "alert('File have been saved.')", True)
End Sub
Screenshot
Downloads