如何使用ASP .NET C将excel文件保存在下载文件夹中 [英] How can I save excel file in downloads folder using ASP .NET C#

查看:47
本文介绍了如何使用ASP .NET C将excel文件保存在下载文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Below is my code to download the data in excel but the problem is while downloading it is not showing that the file is getting downloaded moreover i am giving the path in this way as given below to download the file in downloads folder but i should not use this because it works in local host but it will not work when hosted in server.how can i download into downloads folder with showing the downloading file at bottom







string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
           string pathDownload = Path.Combine(pathUser, "Downloads\\");

protected void btnExportExcel_Click(object sender, EventArgs e)
    {
string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
               string pathDownload = Path.Combine(pathUser, "Downloads\\");
 DataTable dtExcel = (DataTable)ViewState["data"];
ExportToExcel(dsExcel, pathDownload + "\\" + file.xls");
}
 private void ExportToExcel(DataSet table, string filePath)
    {

        int tablecount = table.Tables.Count;
        StreamWriter sw = new StreamWriter(filePath, false);
        sw.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
        sw.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");

        for (int k = 0; k < tablecount; k++)
        {


            sw.Write("<BR><BR><BR>");
            sw.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri; background:'#1E90FF'> <TR>");


            int columnscount = table.Tables[k].Columns.Count;

            for (int j = 0; j < columnscount; j++)
            {
                sw.Write("<Td bgColor='#87CEFA'>");
                sw.Write("");
                //sw.Write(table.Columns[j].ToString());
                sw.Write(table.Tables[k].Columns[j].ToString());

                sw.Write("");
                sw.Write("</Td>");
            }
            sw.Write("</TR>");
            foreach (DataRow row in table.Tables[k].Rows)
            {
                sw.Write("<TR>");
                for (int i = 0; i < table.Tables[k].Columns.Count; i++)
                {
                    sw.Write("<Td>");
                    sw.Write(row[i].ToString());
                    sw.Write("</Td>");
                }
                sw.Write("</TR>");
            }
  sw.Write("</Table>");
            //sw.Write("<BR><BR><BR><BR>");
            //sw.Write("\n");
            //sw.Write(string.Format("Line1{0}Line2{0}", Environment.NewLine));


            sw.Write("</font>");

        }
        sw.Close();
    }







the below two lines that i am using in btnexportexcel should not be used







string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
               string pathDownload = Path.Combine(pathUser, "Downloads\\");





我是什么尝试过:





What I have tried:

he problem is while downloading it is not showing that the file is getting downloaded moreover i am giving the path in this way as given below to download the file in downloads folder but i should not use this because it works in local host but it will not work when hosted in server

推荐答案

您没有将文件下载到客户端,只需将其写入运行代码的机器的文件系统即可。当那是你的本地机器时,看起来它正在工作,但现在代码被部署到服务器,它正在将文件写入服务器。



你可以'从服务器代码将文件保存到客户端系统,想象一下如果网站可以做到这一点会带来安全隐患吗?您需要将文件传输到客户端,以便他们获得下载或打开框,并且您无法指定文件保存在文件中的位置或者是否保存文件。 />


谷歌asp.net下载文件到客户端有关如何执行此操作的示例。
You're not downloading the file to the client, you're simply writing it to the filesystem of the machine the code is running on. When that was your local machine it looked like it was working, but now the code is deployed to a server it is writing the file to the server.

You can't save files to the client system from your server code, imagine the security implications if web sites could do that? You'll need to transmit the file to the client instead so that they'll get the "download or open" box, and you can't specify where on the machine the file is saved or if it is saved at all.

Google "asp.net download file to client" for examples of how to do this.


您不能这样做:下载文件生产中位于您的服务器代码无法访问的单独计算机上。您根本无法使用StreamWriter直接将文件写入用户,并且您无法控制下载开始后会发生什么,因为这完全取决于用户及其浏览器设置。您根本无法为要保存的文件指定文件夹,或者甚至指定应保存文件而不是打开文件。



相反,您要么给用户点击链接:

You cannot do that: download files in production are on a separate computer to which your server code has no access. You cannot use a StreamWriter to write a file directly to the user at all, and you cannot control what happens to it once the download has started as that is totally up to the user and his browser settings. You cannot specify a folder for the file to be saved in at all, or even specify that the file should be saved instead of opened.

Instead you either give teh user a link to click:
<a href="serverFilePath.xlsx" >Click to Save Excel document</a>



或者你通过Response对象发送它:


Or you send it via the Response object:

byte[] fileContent = File.ReadAllBytes(path);
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader("Content-Disposition attachment; filename=myFile.xlsx");
    HttpContext.Current.Response.AddHeader("Content-Length", fileContent.Length.ToString());
    HttpContext.Current.Response.ContentType = "application/octet-stream";
    HttpContext.Current.Response.BinaryWrite(fileContent);
    HttpContext.Current.Response.End();


这篇关于如何使用ASP .NET C将excel文件保存在下载文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆