提交数据都后台生成一个excel文件后怎么弹出另存为菜单保存文件
我现在是直接Redirect到文件地址
结果只能在原页面打开

解决方案 »

  1.   

      System.IO.Stream iStream = null;
                byte[] buffer = new Byte[10000];
                int length;
                long dataToRead;
                try
                {
                    iStream = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
                    dataToRead = iStream.Length;
                   // HttpContext.Current.Response.ContentType = "application/octet-stream";
                    HttpContext.Current.Response.ContentType = "application/ms-excel";
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + BOUtilities.EncodeFileName(exportName));
                    if (dataToRead == 0)
                    {
                        HttpContext.Current.Response.WriteFile(fileName);
                        HttpContext.Current.Response.Flush();
                    }
                    else
                    {
                        while (dataToRead > 0)
                        {
                            if (HttpContext.Current.Response.IsClientConnected)
                            {
                                length = iStream.Read(buffer, 0, 10000);
                                HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
                                HttpContext.Current.Response.Flush();
                                buffer = new Byte[10000];
                                dataToRead = dataToRead - length;
                            }
                            else
                            {
                                dataToRead = -1;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    HttpContext.Current.Response.Write("Error : " + ex.Message);
                }
                finally
                {
                    if (iStream != null)
                    {
                        iStream.Close();
                    }
                    HttpContext.Current.Response.Close();
                    HttpContext.Current.Response.End();
                }