2011-12-22 81 views
1

有人可以告訴我一個很好的方式來獲得文件擴展名,當試圖從給定的URI下載文件? 目前我正在使用WebClient下載文件。 我正在獲取MIME類型並使用它將其映射到擴展。從給定的URL下載文件

這裏這是一個自定義的webclient,這取決於HeadOnly屬性或者返回數據或只是頭。

public class SlideWebClient : WebClient { 
    public bool HeadOnly { get; set; } 
     protected override WebRequest GetWebRequest(Uri address) { 
      WebRequest req = base.GetWebRequest(address); 
      if (HeadOnly && req.Method == "GET") { 
       req.Method = "HEAD"; 
      } 
      return req; 
     } 
    } 
} 


public class FileDownloader {   

    /// <summary> 
    /// Function to download a file from URL and save it to local drive 
    /// </summary> 
    /// <param name="_URL">URL address to download file</param> 
    public static void DownloadFile(Uri source, string destination) { 
     try { 
      using (WebClient _WebClient = new WebClient()) { 
       // Downloads the resource with the specified URI 
       // to a local file. 
       _WebClient.DownloadFile(source, destination); 
      } 
     } catch (Exception _Exception) { 
      // Error 
      Console.WriteLine("Exception caught in process: {0}", 
       _Exception.ToString()); 
     } 
    } 

    /// <summary> 
    ///Get the Content type of file to be downloaded for given URI 
    /// </summary> 
    /// <returns></returns> 
    public static String GetContentType(Uri url) { 
     using (SlideWebClient client = new SlideWebClient()) { 
      client.HeadOnly = true; 
      // note should be 0-length 
      byte[] body = client.DownloadData(url); 
      return client.ResponseHeaders["content-type"]; 
     } 
    } 

    public static bool IsPdf(string contentType) { 
     if (contentType.Contains("application/pdf")) return true; 
     else return false; 
    } 
} 
+0

給出的URI是否包含請求文件名?如果是這樣,你可以將它解析出URI而不必先下載文件。 – 2011-12-22 20:15:08

+0

該網址是否必須包含文件名和文件類型? 'http:// site.com /'是一個完全有效的URL,既沒有文件名也沒有文件類型。 – 2011-12-22 20:29:02

回答

0

如果你只是需要的文件類型,而不是文件,只是看在URI的最後一段,並檢查已知文件類型。這不能保證被設置,所以如果你需要下載文件,那麼mime類型是你最好的選擇。

1

這應該有幫助...我用它來爲客戶下載最新的更新文件。所有你需要的是一個按鈕和一個進度條。

private void btnStartDownload_Click(object sender, EventArgs e) 
    { 
     WebClient client = new WebClient(); 
     client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); 
     client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); 

     client.DownloadFileAsync(new Uri(@"http://www.trendmicro.com/ftp/products/wfbs/WFBS70_EN_GM_B1343.exe"), @"C:\temp\WFBS7.exe"); 
     btnStartDownload.Text = "Download In Process"; 
     btnStartDownload.Enabled = false; 
    } 

    void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
    { 
     double bytesIn = double.Parse(e.BytesReceived.ToString()); 
     double totalBytes = double.Parse(e.TotalBytesToReceive.ToString()); 
     double percentage = bytesIn/totalBytes * 100; 
     progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString()); 
    } 

    void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) 
    { 
     MessageBox.Show("Download Completed"); 
     btnStartDownload.Text = "Start Download"; 
     btnStartDownload.Enabled = true; 
    } 
+0

感謝您的代碼。 – anubhavmag 2011-12-23 12:51:11

+0

感謝您的代碼。但事情是我會下載使用URL的不同類型的文件,所以我不會知道目標文件擴展名。該文件的大部分將是PDF,PPT。 – anubhavmag 2011-12-23 12:57:40