【问题标题】:Where to place the files that can be downloaded by the user用户可以下载的文件放在哪里
【发布时间】:2014-05-04 07:03:33
【问题描述】:

我在做一个项目,遇到了一个问题

我想给用户一个文件来下载所以我写了这个锚标签

<a href="Sample.xls">Download here</a>

问题是当我点击此链接时,浏览器显示 404 错误

我会认为我的机制是错误的,只是当我将它链接到一个 html 文件(存在于同一目录中)时,它工作正常。

<a href="select.html">Download here</a>

这似乎工作正常

是不是因为浏览器不能处理excel文件而不能处理html文件导致链接失效?如果是,我该如何解决这个问题?

P.S.我使用tomcat发布网站,所有资源文件都在web应用的根目录下

【问题讨论】:

    标签: html download anchor


    【解决方案1】:

    点击以下链接。它有答案。

    要下载excel文件你应该提到内容

    在链接按钮上单击事件处理程序添加此代码:

    string path = @getpathfromappconfig + "\\" + FileName + ".xlsx";
    System.IO.FileInfo file = new System.IO.FileInfo(path);
    string Outgoingfile = FileName + ".xlsx"; 
    if (file.Exists)
    { 
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Outgoingfile);
        Response.AddHeader("Content-Length", file.Length.ToString());
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.WriteFile(file.FullName);
    
    }
    else
    {
        Response.Write("This file does not exist.");
    }
    

    【讨论】:

    • 我用 download 尝试了这段代码.. html 文件仍在打开.. 下载是我在其中的功能写了上面的代码..
    • http 协议只知道 html。所以它返回的html文件没有任何修改..对于其他类型的文件,您需要提及内容类型和所有代码作为回答。您使用的是哪种技术 Asp.Net WebForm 或 MVC
    猜你喜欢
    • 1970-01-01
    • 2021-09-09
    • 2015-11-05
    • 1970-01-01
    • 2020-02-26
    • 2017-06-04
    • 2011-03-27
    • 2017-11-19
    • 1970-01-01
    相关资源
    最近更新 更多