WebClient DownloadFile方法下载损坏的PDF文件 [英] WebClient DownloadFile method downloads damaged PDF files

查看:89
本文介绍了WebClient DownloadFile方法下载损坏的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



在我的Windows应用程序中,我使用WebClient DownloadFile方法从本地网络上的服务器下载多个PDF文件。



每个文件都是在调用其URL时生成的报告。报告的大小不同,需要不同的时间才能生成。



我的代码遍历一个URL列表(大约400个),以及它调用的每个URL DownloadFile方法,生成相应的报告并下载到本地机器。网址肯定是正确的。



问题是几乎每次运行应用程序时,一些下载的文件都会被损坏,只下载了7KB(我认为是meta的数据),当我尝试打开文件时,Acrobat Reader给我一条消息:

......它不是支持的文件类型,也不是因为文件已损坏......



并不总是相同的文件被损坏,当我重新运行应用程序时,这些文件经常成功,而其他一些文件可能会失败......它似乎是随机的,我不能找出标准。



注1:在完全下载优先级之前,我不希望文件开始下载,这就是我不使用异步方法的原因。
注2:所有文件都是Oracle报告,并通过查询数据库生成。

注3:如果文件损坏,则不会引发EXCEPTION。



先谢谢。

Dear all,

In my Windows application I am using WebClient DownloadFile method to download several PDF files from a server on local network.

Each file is a report that gets generated when its URL is called. Reports are of different sizes and take different periods of time to get generated.

My code loops through a list of URLs (about 400), and for each URL it calls DownloadFile method, and the corresponding report is generated and downloaded to local machine. URLs are definitely correct.

The problem is that almost each time the application is run, some of downloaded files are damaged, only 7KBs are downloaded (I think it’s for meta data), and Acrobat Reader gives me a message when I try to open the file:
"…it’s either not a supported file type or because the file has been damaged…"

It’s not always the same files that get damaged, and when I re-run the application, those files often succeed, and some others might fail… it seems to be random and I can’t find out the criteria.

Note 1: I don’t want a file to start download until its precedence is completely downloaded, that’s why I am not using the asynchronous method.
Note 2: All files are Oracle reports and get generated by querying a database.
Note 3: No EXCEPTION is thrown in case of damaged files.

Thanks in advance.

推荐答案

亲爱的谢尔盖,这是我的代码。这很简单。





Dear Sergey, this is my code. it's simple.


using ( WebClient client  = new WebClient() )
{
    for(int i=0; i< URL_List.Length; i++)
    {
        try
        {
        client.DownloadFile( URL_List[i] , myLocalPath+fileName+".pdf" );
        }
        catch(Exception x)
        {
         // write exception message to error log...
        }
    }
}


我已经找到了问题所在。某些报告由于某种原因而未正确生成 - 因此,返回带有错误消息的HTML页面而不是PDF文件,因此它不是可由acrobat reader打开的受支持的文件类型。当我将损坏文件扩展名从.pdf更改为.html时,它们在浏览器中打开,我可以读取Oracle返回的错误。

谢谢大家。
I have figured out the problem. Some reports are just not generated correctly -for some reason- and hence, an HTML page with error message is returned instead of PDF file, so it's not a supported file type that can be opened by acrobat reader. When i changed "damaged" files extension from .pdf to .html , they opened in the browser and i could read the error returned by Oracle.
thank you everyone.


这篇关于WebClient DownloadFile方法下载损坏的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆