将文件存储在 ASP.net 网站中,然后下载到浏览器

发布于 2024-09-17 06:56:15 字数 1646 浏览 5 评论 0原文

我正在我的 asp.net 网站上用 C# 创建一个 Excel 文件。然后,我想将该文件保存在 Web 服务器文件中的某个位置,并将该文件发送到浏览器以供用户下载。

我已经让它在开发环境中的本地系统上运行,但我没有得到正确的文件寻址。

  1. 如何将文件存储在“~\ParentFolder\SubFolder\file.ext”中。
  2. 然后将该文件发送到浏览器以供用户下载。

任何指导将不胜感激。

        String outputFile = Utilities.writeToExcelFile("", MapPath(@"~\ReportFiles\TempFiles\DropShipData.xls"), table);

    DownloadFile(outputFile);


       public void DownloadFile(string fname)
{
    string path = fname;
    string name = Path.GetFileName(path);
    string ext = Path.GetExtension(path);
    string type = "";
    // set known types based on file extension  
    if (ext != null)
    {
        switch (ext.ToLower())
        {
            case ".htm":
            case ".html":
            case ".aspx":
            case ".asp":
                type = "text/HTML";
                break;

            case ".xls":
            case ".xlsx":
            case ".csv":
                type = "Application/x-msexcel";
                break;

            case ".pdf":
                type = "Application/pdf";
                break;

            case ".txt":
                type = "text/plain";
                break;

            case ".doc":
            case ".docx":
            case ".rtf":
                type = "Application/msword";
                break;
        }
    }

    Response.AppendHeader("content-disposition",
        "attachment; filename=" + name);

    if (type != "")
        Response.ContentType = type;
    Response.WriteFile(path);
    Response.End();
}

同样,这在我的本地电脑上运行良好,但是当我将其移动到服务器时,我收到访问路径的错误。错误代码中列出的路径不是我想要文件所在的位置。

I'm creating an Excel file in C# on my asp.net web site. Then, I want to save this file somewhere within the web server's files, and send that file to the browser for the user to download.

I've got it working on my local system in the dev environment, but I'm not getting the file addressing right.

  1. How can I store a file in "~\ParentFolder\SubFolder\file.ext".
  2. Then send that file to the browser for user download.

Any guidance would be much appreciated.

        String outputFile = Utilities.writeToExcelFile("", MapPath(@"~\ReportFiles\TempFiles\DropShipData.xls"), table);

    DownloadFile(outputFile);


       public void DownloadFile(string fname)
{
    string path = fname;
    string name = Path.GetFileName(path);
    string ext = Path.GetExtension(path);
    string type = "";
    // set known types based on file extension  
    if (ext != null)
    {
        switch (ext.ToLower())
        {
            case ".htm":
            case ".html":
            case ".aspx":
            case ".asp":
                type = "text/HTML";
                break;

            case ".xls":
            case ".xlsx":
            case ".csv":
                type = "Application/x-msexcel";
                break;

            case ".pdf":
                type = "Application/pdf";
                break;

            case ".txt":
                type = "text/plain";
                break;

            case ".doc":
            case ".docx":
            case ".rtf":
                type = "Application/msword";
                break;
        }
    }

    Response.AppendHeader("content-disposition",
        "attachment; filename=" + name);

    if (type != "")
        Response.ContentType = type;
    Response.WriteFile(path);
    Response.End();
}

Again, this works fine on my local pc, but when I move it to the server I get an error for accessing the path. And the path listed in the error code is NOT where I want the file to go.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南城旧梦 2024-09-24 06:56:15

首先存储文件:

string physicalPath = Server.MapPath("~/ParentFolder/SubFolder/file.ext");
// returns c:\path\to\website\ParentFolder\SubFolder\file.ext

然后您可以告诉浏览器重定向到该文件

Response.Redirect("~/ParentFolder/SubFolder/file.ext");

First store the file:

string physicalPath = Server.MapPath("~/ParentFolder/SubFolder/file.ext");
// returns c:\path\to\website\ParentFolder\SubFolder\file.ext

Then you could for example, tell the browser to redirect to that file

Response.Redirect("~/ParentFolder/SubFolder/file.ext");
亽野灬性zι浪 2024-09-24 06:56:15

您可能还需要检查并确保运行 ASP.Net 辅助进程的帐户或相应的用户(如果您使用模拟)具有对 ReportFiles\TempFiles 位置的写入权限。很多时候会发生错误,因为默认权限只授予对网站中文件夹的读取权限。

You might also want to check and make sure that the account running the ASP.Net worker process, or the appropriate user if you're using Impersonation, has write privileges to the ReportFiles\TempFiles location. A lot of times the error occurs because the default privileges only give read access to the folders in your website.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文