This article for you to share an example code about the CSS implementation of file download function, the need for a friend to refer to it
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 OH) Encoding Encoding; Declaration code string OutputFileName; Output name Debug.Assert (HttpContext.ApplicationInstance.Request.UserAgent! = NULL, " HttpContext.ApplicationInstance.Request.UserAgent = null "); string browser = HttpContext.ApplicationInstance.Request.UserAgent.ToUpper ();//Microsoft's 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
Front-end HTML to write a tag is good:<a href='DownFile' target='_blank'>文件下载</a>
【相关推荐】
1. 特别推荐:“php程序员工具箱”V0.1版本下载
2. ASP免费视频教程
3. 李炎恢ASP基础视频教程