本文實(shí)例講述了ASP.NET實(shí)現(xiàn)推送文件到瀏覽器的方法。。具體分析如下:
這里主要實(shí)現(xiàn)從服務(wù)器到瀏覽器,推送文件,提供用戶下載/瀏覽的功能。
提示: 在AJAX UpdatePanel里面將無效。如果代碼從按鈕單擊事件中被調(diào)用,該按鈕需要在 AJAX UpdatePanel的外部。
具體代碼如下:
/// <summary>/// Downloads (pushes) file to the client browser. /// **** NOTE **** Cannot be done from inside an AJAX UpdatePanel control./// </summary>/// <param name="fullFilePath">The full file path of the file</param>protected void DownloadFile(string fullFilePath){ // Gets the File Name string fileName = fullFilePath.Substring(fullFilePath.LastIndexOf('//') + 1); byte[] buffer; using (FileStream fileStream = new FileStream(fullFilePath, FileMode.Open)) { int fileSize = (int)fileStream.Length; buffer = new byte[fileSize]; // Read file into buffer fileStream.Read(buffer, 0, (int)fileSize); } Response.Clear(); Response.Buffer = true; Response.BufferOutput = true; Response.ContentType = "application/x-download"; Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); Response.CacheControl = "public"; // writes buffer to OutputStream Response.OutputStream.Write(buffer, 0, buffer.Length); Response.End();}希望本文所述對大家的asp.net程序設(shè)計有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選