简体   繁体   English

HTML 5定位标记下载的文件不完整?

[英]HTML 5 anchor tag download incomplete file?

I am using angular and ASP.NET Web API to allow users to download files that are generated on the server. 我正在使用angular和ASP.NET Web API允许用户下载服务器上生成的文件。

HTML Markup for download link: HTML标记下载链接:

 <img src="/content/images/table_excel.png">
 <a ng-click="exportToExcel(report.Id)">Excel Model</a>
 <a id="report_{{report.Id}}" target="_self"></a>

The last anchor tag is there to serve as a place holder for an automatic click event. 最后的锚标记在那里用作自动单击事件的占位符。 The visible anchor calls the exportToExcel method to initiate the call to the server and begin creating the file. 可见的锚点调用exportToExcel方法以发起对服务器的调用并开始创建文件。

$scope.exportToExcel = function(reportId) {
    reportService.excelExport(reportId, function (result) {
        var url = "/files/report_" + reportId + "/" + result.data.Model.fileName;
        var dLink = document.getElementById("report_" + reportId);
        dLink.href = url;
        dLink.setAttribute('download', result.data.Model.fileName);
        dLink.click();
    });
}

The Web API code creates an Excel file. Web API代码创建一个Excel文件。 The file, on the server is about 279k, but when it is downloaded on the client it is only 7k. 服务器上的文件约为279k,但在客户端上下载时仅为7k。 My first thought was that the automatic click might be happening before the file is completely written. 我首先想到的是,在文件完全写入之前,可能会发生自动单击。 So, I added a 10 second $timeout around the click event as a test. 因此,我在click事件周围添加了10秒的$timeout作为测试。 It failed with the same result. 它失败,结果相同。

This seems to only be happening on our remote QA server. 这似乎仅在我们的远程质量检查服务器上发生。 On my local development server I always get the entire file back. 在本地开发服务器上,我始终会取回整个文件。 I am at a loss as to why this might be happening. 我不知道为什么会发生这种情况。 We have similar functionality where files are constructed from a database blob and saved to the local disk for download. 我们具有类似的功能,即从数据库Blob构造文件并将其保存到本地磁盘以供下载。 The same method is employed for the client side download and that seems to work fine. 客户端下载使用相同的方法,这似乎很好用。 I am wondering if anyone else has run into a similar issue. 我想知道是否还有其他人遇到过类似问题。

Update After the comment by SilentTremmor we think it actually may be IIS or some sort of Sever issue. 更新在SilentTremmor发表评论后,我们认为它实际上可能是IIS或某种Sever问题。 Originally, we didn't think it could be, but after some digging it may be. 最初,我们认为不可能,但是经过一番挖掘后,也许是可行的。 It seems the instance of the client code is only allowing 7k of data to be downloaded. 客户端代码的实例似乎只允许下载7k数据。 It doesn't matter what we try to download the result is always the same. 不管我们尝试下载什么,结果都是一样的。

It turns out the API application was writing the file to a different instance of our application. 事实证明,API应用程序正在将文件写入我们应用程序的其他实例。 The client code had no idea and was trying to download a file that did not exist. 客户端代码不知道,并且正在尝试下载不存在的文件。 So, when the download link was creating the file it was empty, thus the small file size. 因此,当下载链接创建文件时,该文件为空,因此文件较小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM