如何避免将文件下载到下载文件夹,并仅在c#中下载到其他特定文件夹

本文关键字:文件夹 下载 其他 何避免 文件下载 | 更新日期: 2023-09-27 18:28:43

我正在尝试将gridview转换为excel,并将excel表下载到我的c#应用程序中计算机的特定文件夹中。

问题是:文件在这两个地方都被下载了。目标文件夹以及下载文件夹。

这个的代码是:

private void ExportToExcel(GridView GrdView, string fname)
{
    Response.Clear();
    Response.AddHeader("content-disposition", "inline;filename=" + fname + ".xls");
    Response.Charset = "";
    Response.ContentType = "application/OCTET-STREAM";
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite =
    new HtmlTextWriter(stringWrite);
    GridView1.RenderControl(htmlWrite);
    string renderedGridView = stringWrite.ToString();
    File.WriteAllText(@"C:''Users'abc''Desktop'" + fname + ".xls", renderedGridView);
    Response.Write(stringWrite.ToString());
    Response.End();
}

如何避免文件被下载到下载文件夹?请帮忙!感谢

如何避免将文件下载到下载文件夹,并仅在c#中下载到其他特定文件夹

答案是选择一个:将控件呈现为字符串并输出到Response或WriteAllText。如果要使用WriteAllText将文件保存到确定的位置,请执行该操作并向用户弹出文件已保存的通知。