<small id='rUtLc'></small><noframes id='rUtLc'>

        <i id='rUtLc'><tr id='rUtLc'><dt id='rUtLc'><q id='rUtLc'><span id='rUtLc'><b id='rUtLc'><form id='rUtLc'><ins id='rUtLc'></ins><ul id='rUtLc'></ul><sub id='rUtLc'></sub></form><legend id='rUtLc'></legend><bdo id='rUtLc'><pre id='rUtLc'><center id='rUtLc'></center></pre></bdo></b><th id='rUtLc'></th></span></q></dt></tr></i><div id='rUtLc'><tfoot id='rUtLc'></tfoot><dl id='rUtLc'><fieldset id='rUtLc'></fieldset></dl></div>

          <bdo id='rUtLc'></bdo><ul id='rUtLc'></ul>
        <tfoot id='rUtLc'></tfoot>
      1. <legend id='rUtLc'><style id='rUtLc'><dir id='rUtLc'><q id='rUtLc'></q></dir></style></legend>
      2. 获取没有 Content-Disposition 的文件名

        时间:2023-08-26

            1. <i id='xqXLG'><tr id='xqXLG'><dt id='xqXLG'><q id='xqXLG'><span id='xqXLG'><b id='xqXLG'><form id='xqXLG'><ins id='xqXLG'></ins><ul id='xqXLG'></ul><sub id='xqXLG'></sub></form><legend id='xqXLG'></legend><bdo id='xqXLG'><pre id='xqXLG'><center id='xqXLG'></center></pre></bdo></b><th id='xqXLG'></th></span></q></dt></tr></i><div id='xqXLG'><tfoot id='xqXLG'></tfoot><dl id='xqXLG'><fieldset id='xqXLG'></fieldset></dl></div>
            2. <tfoot id='xqXLG'></tfoot>

              <small id='xqXLG'></small><noframes id='xqXLG'>

                <legend id='xqXLG'><style id='xqXLG'><dir id='xqXLG'><q id='xqXLG'></q></dir></style></legend>
                • <bdo id='xqXLG'></bdo><ul id='xqXLG'></ul>
                    <tbody id='xqXLG'></tbody>

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

                  问题描述

                  我几天来一直在寻找这个问题的解决方案,但我找不到任何解决方案.

                  I am looking for a solution for this problem for days and I can't find any.

                  我想通过网络客户端从网络服务器下载文件.下载工作正常,但我无法获得真实的文件名,这对我来说非常重要.

                  I want to download a file from a webserver with a webclient. The download works fine, but I can't get the real filename, which is very important for me.

                  我在许多主页上读到,文件名应该保存在 Content-Disposition-Header 中.不幸的是,这个网站的标题是空的.我试图得到它:

                  I read on many homepages, that the filename should be saved in the Content-Disposition-Header. Unfortunately this header of the site is empty. I tried to get it with:

                  string header_contentDisposition ="";
                  using (WebClient client = new WebClient())
                              {
                                  client.OpenRead(link);
                  
                                  header_contentDisposition = client.ResponseHeaders["Content-Disposition"];
                                  MessageBox.Show(header_contentDisposition);
                              }
                  

                  此标头内没有保存任何信息.

                  There is no information saved inside this header.

                  如果我尝试使用浏览器(IE、Opera、Chrome)下载文件,文件名会显示在文件对话框中,因此必须将其保存在某处.

                  If I try to download the file with my Browser (IE, Opera, Chrome) the filename gets shown in the filedialog, so it has to be saved somewhere.

                  你能想象我在哪里可以找到它吗?

                  Can you imagine where I can find it?

                  我无法从 URL 中提取它,因为链接是由 php 生成的,例如

                  I can't extract it from the URL, because the link is generated by php like

                  http://www.example.com/download.php?id=10
                  

                  推荐答案

                  你可以试试这个:

                          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                          try
                          {
                  
                              HttpWebResponse res = (HttpWebResponse)request.GetResponse();
                              using (Stream rstream = res.GetResponseStream())
                              {
                                  string fileName = res.Headers["Content-Disposition"] != null ?
                                      res.Headers["Content-Disposition"].Replace("attachment; filename=", "").Replace(""", "") :
                                      res.Headers["Location"] != null ? Path.GetFileName(res.Headers["Location"]) : 
                                      Path.GetFileName(url).Contains('?') || Path.GetFileName(url).Contains('=') ?
                                      Path.GetFileName(res.ResponseUri.ToString()) : defaultFileName;
                              }
                              res.Close();
                          }
                          catch { }
                  

                  在 http://upload.p-kratzer.com/index.php?dir=&file=asdfasdfwervdcvvedb 它确实返回了 wetter.JPG.

                  Tested this on http://upload.p-kratzer.com/index.php?dir=&file=asdfasdfwervdcvvedb and it does return wetter.JPG.

                  这篇关于获取没有 Content-Disposition 的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:ASP.NET 母版页:如何在 aspx 内的 head 部分插入标记 下一篇:为 C# 中的所有请求向 Webbrowser 控件添加自定义标

                  相关文章

                  最新文章

                • <i id='VfYqH'><tr id='VfYqH'><dt id='VfYqH'><q id='VfYqH'><span id='VfYqH'><b id='VfYqH'><form id='VfYqH'><ins id='VfYqH'></ins><ul id='VfYqH'></ul><sub id='VfYqH'></sub></form><legend id='VfYqH'></legend><bdo id='VfYqH'><pre id='VfYqH'><center id='VfYqH'></center></pre></bdo></b><th id='VfYqH'></th></span></q></dt></tr></i><div id='VfYqH'><tfoot id='VfYqH'></tfoot><dl id='VfYqH'><fieldset id='VfYqH'></fieldset></dl></div>
                  <legend id='VfYqH'><style id='VfYqH'><dir id='VfYqH'><q id='VfYqH'></q></dir></style></legend>
                      <bdo id='VfYqH'></bdo><ul id='VfYqH'></ul>

                    1. <tfoot id='VfYqH'></tfoot>

                    2. <small id='VfYqH'></small><noframes id='VfYqH'>