下载时获取文件名 [英] Get filename while downloading it

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

问题描述

我们提供保存在我们的数据库中的文件,唯一的方法是通过他们的 id ,如下所示:

We are providing files that are saved in our database and the only way to retrieve them is by going by their id as in:

www.AwesomeURL.com/AwesomeSite.aspx?requestedFileId=23

一切都是正在使用 WebClient Class 的工作文件。

Everything is working file as I am using the WebClient Class.

我只面对一个问题:

如何获得真实 filename?

How can I get the real filename?

我的代码看起来像这个atm:

My code looks like this atm:

WebClient client = new WebClient ();

string url = "www.AwesomeURL.com/AwesomeSite.aspx?requestedFileId=23";

client.DownloadFile(url, "IDontKnowHowToGetTheRealFileNameHere.txt");

我知道的只是 id

当我尝试从浏览器访问 url 时,不会发生这种情况,它的正确名称=> DownloadedFile.xls

This does not happen when I try accessing url from the browser where it get's the proper name => DownloadedFile.xls.

获得正确答案的正确方式是什么?

What's the proper way to get the correct response?

推荐答案

我有同样的问题,我发现这个类: System.Net.Mime.ContentDisposition

I had the same problem, and I found this class: System.Net.Mime.ContentDisposition.

using (WebClient client = new WebClient()){
    client.OpenRead(url);

    string header_contentDisposition = client.ResponseHeaders["content-disposition"];
    string filename = new ContentDisposition(header_contentDisposition).FileName;

    ...do stuff...
}

类文档表明它适用于电子邮件附件,但在我以前测试的服务器上工作正常,避免解析非常好。

The class documentation suggests it's intended for email attachments, but it works fine on the server I used to test, and it's really nice to avoid the parsing.

这篇关于下载时获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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