简体   繁体   English

从生成文件的URL下载

[英]Download from url that generates a file

I need to be able to download a file from url. 我需要能够从url下载文件。 The url does not link to an actual file instead it generates the file on the server first and then gives a download dialog. 该url不会链接到实际文件,而是先在服务器上生成该文件,然后提供一个下载对话框。 Probably returning an mvc FileResult. 可能返回一个mvc FileResult。

I'm just interested in getting the byte[] from the file. 我只是想从文件中获取byte []。

I've tried: 我试过了:

        using (var webClient = new WebClient())
        {
            System.Uri uri = new System.Uri(Document.Url);
            bytes = await webClient.DownloadDataTaskAsync(uri);
        }

This works but I get a corrupted file as expected. 这可行,但是我得到的文件已损坏。

I do not have control over how the server generates or serves the file. 我无法控制服务器如何生成或提供文件。

Any way to wait for the file to complete generating and then get the file content? 有什么方法等待文件完成生成然后获取文件内容?

TIA TIA

Never mind. 没关系。 Turns out the link returns some javascript that auto authenticates a user and then does a jquery get to another url and port to generate the file. 原来,该链接返回了一些可自动验证用户身份的javascript,然后通过jquery到达另一个url和端口以生成文件。 So I was basically downloading that script and saving it to pdf. 所以我基本上是下载该脚本并将其保存为pdf。 doh. h

So a work around would be to mimic that in some way. 因此,一种解决方法是以某种方式模仿它。

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

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