开发者

C# file download using WebBrowser

Problem and solution is available at http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/d338a2c8-96df-4cb0-b8be-c5fbdd7c9202/?prof=required

开发者_运维技巧

This work perfectly if there is direct URL including downloading file-name.

But sometime some URL generate file dynamically. So URL don't have file name but after requesting that URL some website create file dynamically and then open/save dialog comes.

for example some link generate pdf file on the fly.

How to handle such type of URL?


If you have control over the server code, then you could ensure that it adds the Content-Disposition header to the HTTP response, such as the following:

Content-Disposition: attachment; filename=<put your filename here>

You could then look for that header on the client in order to decide to download the file in the background.

Even if you don't have control over the server code, check if the HTTP response already contains this header. I believe that this is a standard header that most browsers look for in order to know how to download a file.

This page indicates some guidelines for using the Content-Disposition header, including the following items:

  • The filename should be in US-ASCII charset.

  • The filename should not have any directory path information specified.

  • The filename should not be enclosed in double quotes even though most browsers will support it.

  • Content-Type header should be before Content-Disposition.

  • Content-Type header should refer to an unknown MIME type (at least until the older browsers go away).

Scott Hanselman also has a small article on using this header.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜