Get the App
SLTechnology News&Howtos  ›  Development  › 

How C # is compatible with file downloads of major browsers

Shulou Source: shulou.com Published: 2022-06-03 16:34:35 09月22日 Update

This article mainly introduces how C# is compatible with the file downloads of major browsers, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

1. Css code

Public void DownFile (string filePath, string fileName) {/ / filePath file path for example: / File/ record .xlsx / / fileName file name for example: record .xlsx (to suffix) Encoding encoding; / / declare coding string outputFileName; / / output name Debug.Assert (HttpContext.ApplicationInstance.Request.UserAgent! = null, "HttpContext.ApplicationInstance.Request.UserAgent! = null"); string browser = HttpContext.ApplicationInstance.Request.UserAgent.ToUpper () / Microsoft browser and ie filter if (browser.Contains ("MS") & & browser.Contains ("IE")) {outputFileName = HttpUtility.UrlEncode (filePath); encoding = Encoding.Default;} / / Firefox else if (browser.Contains ("FIREFOX")) {outputFileName = fileName;encoding = Encoding.GetEncoding ("GB2312");} else {outputFileName = HttpUtility.UrlEncode (fileName); encoding = Encoding.Default;} string absoluFilePath = Server.MapPath (filePath) / / get the upload file path FileStream fs = new FileStream (absoluFilePath, FileMode.Open); byte [] bytes = new byte [(int) fs.Length]; fs.Read (bytes, 0, bytes.Length); fs.Close (); / / close the stream and release the resource HttpContext.ApplicationInstance.Response.Clear (); HttpContext.ApplicationInstance.Response.Buffer = true;HttpContext.ApplicationInstance.Response.ContentEncoding = encoding;HttpContext.ApplicationInstance.Response.AddHeader ("Content-Disposition", string.Format ("attachment") Filename= {0} ", string.IsNullOrEmpty (outputFileName)? DateTime.Now.ToString ("yyyyMMddHHmmssfff"): outputFileName); Response.BinaryWrite (bytes); Response.Flush (); HttpContext.ApplicationInstance.Response.End ();}

2. Html code

Just write an a tag on the front-end HTML: such as file download

Thank you for reading this article carefully. I hope the article "how C# is compatible with the file downloads of major browsers" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Files articles file downloads browsers browsing clips code paths values interests front ends at the same time names suffixes more friends tags Firefox knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei NVidia Shulou Information macOS vpn