Convert Uploaded file to Base64String in C#

We will go through steps to convert a uploaded file to Base64String in C#.

Add a File Upload control to aspx page( for example with ID = CntlFileUpload )
Add a button to aspx page

Add the below code to convert uploaded file to Base64String in Button click event

protected void Button1_Click(object sender, EventArgs e)

{

HttpPostedFile postedFile = this.CntlFileUpload.PostedFile;

Byte[] fileBytes = new Byte[postedFile.ContentLength];

postedFile.InputStream.Read(fileBytes, 0, postedFile.ContentLength);

string base64string = Convert.ToBase64String(fileBytes);

}

Please let me know if this is helpful.

One thought on “Convert Uploaded file to Base64String in C#

Leave a Reply