在Asp.Net中另存为下载。

编程入门 行业动态 更新时间:2024-10-26 08:32:43
本文介绍了在Asp.Net中另存为下载。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我正在做一个基于网络的餐厅管理系统。现在,我已经陷入了将日常销售导出到Quick Book Integration的CSV文件的困境。我想要做的是当我点击导出按钮时,浏览器应该提示另存为对话框以将特定文件保存到目的地。 首先我生成具有临时文件名的文件,然后尝试调用该文件以供用户下载/保存在某个位置,或者文件应该下载到特定的文件夹。桌面,文档等。我尝试的代码如下。 当我执行此代码时,它什么都不返回。但是在它显示的异常内部。 无法评估表达式,因为代码已优化或本机框架位于调用堆栈之上 和System.Threading.ThreadAbortException内部消息说 线程正在中止。

Hi everyone, I'm doing a web based Restaurant management system. Now I've got stuck in exporting the daily sales to a CSV file for the Quick Book Integration. What I want to do is when I click the Export button the browser should be prompt a Save As Dialog to Save the particular file to a destination. For that first I generate the file with a temporary file name and then try to call up that file for the user to download/save in a location or the file should download to a particular folder.i.e Desktop, Document etc. The code I tried is below. When I execute this code it returns nothing. But inside the exception it shows. "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack" And System.Threading.ThreadAbortException says inside message "Thread was being aborted."

string currentDay = (" " + DateTime.Now.Day + "." + DateTime.Now.Month + "." + DateTime.Now.Year + " ").ToString(); string filepathththe = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string tempFilePath = Path.GetTempPath(); string tempFileName = Path.GetTempFileName(); File.WriteAllLines(tempFileName, lines); ////Write the data to temp file string fileName = "SalesDetails.csv"; string filePath = tempFileName; Response.Clear(); Response.ContentType = "text/csv"; Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); Response.TransmitFile(filePath); Response.End();

如何操作?谁可以帮我这个事? 提前致谢。

How to do it? Can anyone help me on this? Thanks in advance.

推荐答案

将此代码用作Class&根据您的要求调用静态方法 Use This Code As Class & Call Static Method As Per Your Requirement using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; using System.IO; using System.Net.Mail; /// <summary> /// Summary description for ExportToEXCEL /// </summary> public class ExportToEXCEL { public ExportToEXCEL() { // // TODO: Add constructor logic here // } public static void ExportGridViewToEXCEL(GridView gv, string FileNameWithEXT) { string attachment = "attachment; filename=" + FileNameWithEXT.Replace(" ", "_") + ""; HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.AddHeader("content-disposition", attachment); HttpContext.Current.Response.ContentType = "application/ms-excel"; StringWriter stw = new StringWriter(); HtmlTextWriter htextw = new HtmlTextWriter(stw); gv.RenderControl(htextw); HttpContext.Current.Response.Write(stw.ToString()); HttpContext.Current.Response.End(); } public static void ExportDtToEXCEL(DataTable DT, string FileNameWithEXT) { DataTable dt = DT; string attachment = "attachment; filename=" + FileNameWithEXT.Replace(" ", "_") + ""; HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.AddHeader("content-disposition", attachment); HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; string tab = ""; foreach (DataColumn dc in dt.Columns) { HttpContext.Current.Response.Write(tab + dc.ColumnName); tab = "\t"; } HttpContext.Current.Response.Write("\n"); int i; foreach (DataRow dr in dt.Rows) { tab = ""; for (i = 0; i < dt.Columns.Count; i++) { HttpContext.Current.Response.Write(tab + Utility.RemoveSpecialCharactersAddress(dr[i].ToString()).Replace("\n", "").Replace(""", "")); tab = "\t"; } HttpContext.Current.Response.Write("\n"); } HttpContext.Current.Response.End(); } public static void ExportDtToXML(DataTable DT, string DataTableName) { DataTable dt = DT; dt.TableName = DataTableName.Replace(" ", "_"); dt.WriteXml(HttpContext.Current.Server.MapPath(".") + @"\finalData.xls", XmlWriteMode.IgnoreSchema); HttpContext.Current.Response.Redirect("finalData.xls"); } private void ExcelExport(HtmlGenericControl DivHTMLTable, string FileNameWithEXT) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + FileNameWithEXT.Replace(" ", "_") + ""); HttpContext.Current.Response.Charset = ""; // If you want the option to open the Excel file without saving than un comment the line below // Response.Cache.SetCacheability(HttpCacheability.NoCache); HttpContext.Current.Response.ContentType = "application/vnd.xls"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); DivHTMLTable.RenderControl(htmlWrite); HttpContext.Current.Response.Write(stringWrite.ToString()); HttpContext.Current.Response.End(); } private static System.IO.MemoryStream ExportToStream(DataTable DT) { string attachment = "attachment; filename=ABC.XLS"; HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.AddHeader("content-disposition", attachment); HttpContext.Current.Response.ContentType = "application/ms-excel"; System.IO.StringWriter sw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw); HtmlForm form = new HtmlForm(); GridView gv = new GridView(); gv.DataSource = DT; gv.DataBind(); form.Controls.Add(gv); gv.RenderControl(hw); string content = sw.ToString(); byte[] byteData = Encoding.Default.GetBytes(content); System.IO.MemoryStream mem = new System.IO.MemoryStream(); mem.Write(byteData, 0, byteData.Length); mem.Flush(); mem.Position = 0; //reset position to the begining of the stream return mem; } public static Attachment DataTableAsAttchment(DataTable DT, string FileNameWithEXT) { System.IO.MemoryStream ms = ExportToStream(DT); Attachment attachFile = new Attachment(ms, FileNameWithEXT.Replace(" ", "_"), "application/vnd.ms-excel"); return attachFile; } public static Attachment DataTableAsAttchmentForGlobalASAX(DataTable DT, string FileNameWithEXT) { System.IO.MemoryStream ms = ExportToStreamForGlobalASAX(DT); Attachment attachFile = new Attachment(ms, FileNameWithEXT, "application/vnd.ms-excel"); return attachFile; } private static System.IO.MemoryStream ExportToStreamForGlobalASAX(DataTable DT) { string attachment = "attachment; filename=ABC.XLS"; System.IO.StringWriter sw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw); HtmlForm form = new HtmlForm(); GridView gv = new GridView(); gv.DataSource = DT; gv.DataBind(); form.Controls.Add(gv); gv.RenderControl(hw); string content = sw.ToString(); byte[] byteData = Encoding.Default.GetBytes(content); System.IO.MemoryStream mem = new System.IO.MemoryStream(); mem.Write(byteData, 0, byteData.Length); mem.Flush(); mem.Position = 0; //reset position to the begining of the stream return mem; } }

更多推荐

在Asp.Net中另存为下载。

本文发布于:2023-07-27 03:07:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219980.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:另存为   Asp   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!