SoFunction
Updated on 2025-03-06

C# Use Base64 to implement file upload

Base64 is one of the most common encoding methods on the network for transmitting 8Bit bytecode. It is a method of representing binary data based on 64 printable characters.

The specific process of using base64 to upload files is: the front desk uses js to convert files to base64 format, and the background uses a high-level programming language to convert files in base64 format to the original file. Let’s demonstrate how to upload, parse and save images in base64 format with JS.

First, let’s take a look at how the front desk reads the file. Please first look at the following js code:

var DataforUp = "";
 var reader = new FileReader(); //Declare file reading object//Declare the method called after the file is read, and obtain the converted data object  = function () {
  DataforUp = ;
 };
// When the user selects a file, perform the operation of converting data $("#file").change(function () {
  ([0]);
 });

The file reading requires the help of FileReader object, DataforUp is used to save base64 source code. #file The id corresponds to the file selection tag.

The FileReader object has an onload event. This event will be triggered when reading the file (or after calling the readAsDataURL method), and the source code converted by base64 exists in its result attribute.

The following is a detailed explanation of the above code:

After the user selects a file, use the FileReader object to read the file, and automatically convert it to base64 format after reading, and then trigger the onload method of the object to save the converted base64 source code.

In this way, we get the file source code in base64 format, and through ajax, we can send the file to the background.

Next, let’s take a look at how the background parses and saves it:

public string SaveImgBaseSixFour(string img,string obj)
{
 if(img==null){
 return "Data is empty";
 }
 string result = img;
 int index = -1;
 //Judge whether it is the base64 file type index = ("base64,");
 if (index != -1)
 {
  index += 7;
 //Convert data to binary byte array var imgbit = Convert.FromBase64String((index));
 // Generate file name string imgname = ("yyyyMMddHHmmss") + ".jpg";
 //Save the picture using (Image image=(new MemoryStream(imgbit)))
  {
   (_rootPath+imgname,);
  }
 }
}

The basic model of base64 source code is data:image/png;base64,...file code...=

data identifies the file type, followed by base64, and the source code of the beginning file ends with =, so when parsing, you must remove the beginning part. The image extension in this case is written to death, and you can also obtain the image extension through the base64 file header. This will not be introduced in detail here.

Analyze the code flow:

Determine whether the file is empty, then get the start index of the real data, then call the Convert.FromBase64String method to convert base64 into the original file, and then save the file data saved in memory to the local area through the file stream. Readers can check the official documents for the specific usage method by themselves, and will not be repeated here.

The above is the detailed content of c# using Base64 to implement file upload. For more information about c# Base64 file upload, please follow my other related articles!