SoFunction
Updated on 2025-03-06

C# file download instance code (applicable to each browser)

1. Cs code

public void DownFile(string filePath ,string fileName )
{
 // filePath file path For example: /File/Record.xlsx // fileName file name For example: record.xlsx (requires suffix)Encoding encoding; // Declare codestring outputFileName; // Output name( != null, " != null");
string browser = ();
// Microsoft's browser and ie filteringif (("MS") && ("IE"))
{
outputFileName = (filePath);
encoding = ;
}
//Firefoxelse if (("FIREFOX"))
{
outputFileName = fileName;
encoding = ("GB2312");
}
else
{
outputFileName = (fileName);
encoding = ;
}
string absoluFilePath = (filePath); //Get the upload file pathFileStream fs = new FileStream(absoluFilePath, );
byte[] bytes = new byte[(int)];
(bytes, 0, );
(); //Close the flow and release the resources();
 = true;
 = encoding;
("Content-Disposition", ("attachment; filename={0}", (outputFileName) ? ("yyyyMMddHHmmssfff") : outputFileName));
(bytes);
();
();
}

2. HTML code

Just write a tag in front-end HTML:<a href='DownFile' target='_blank'>File download</a>

The above is the C# file download example code (applicable to various browsers) introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!