2014-11-25 2 views
-2

У меня есть сайт в РНР этот кодПолучить имя файла из URL с адресом заголовка PHP

switch($type){ 
    case 'check': 
     switch($action){ 
      case 'update': 
       echo "1.0.0.1"; 
      break; 
     } 
    break; 
    case 'download': 
     $file = './ss/godzila.avi'; 
     if (file_exists($file)) { 
      header("Location: {$file}"); 
      exit; 
     }else{ 
      header("HTTP/1.0 404 Not Found"); 
     } 
    break; 
} 

и как я получаю в C#, чтобы имя файла? Этот файл является только тестом. Я пошлю обновления для SW с сервера обновлений.

Мне нужно имя файла для этого в C#:

FileStream newFile = new FileStream(filePatch+fileName, FileMode.Create); 
newFile.Write(downloadedData, 0, downloadedData.Length); 
+0

Вы имеете в виду, вы хотите, чтобы загрузить файл с сервера где-нибудь? – Icepickle

+0

yes i скачать файлы с сервера лицензий/обновлений –

+0

И имя файла находится в заголовке? – Icepickle

ответ

0

Вы могли бы просто проверить HttpWebResponse.Headers при загрузке файла, они должны содержать заголовки посланное вами вместе.

Я думаю, пример вы могли бы использовать этот один, я адаптировать его немного для вашего конкретного использования (нл: имя файла, что вам нужно получить)

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Text; 

namespace wget 
{ 
    class Program 
    { 
     static bool DownloadFile(string url, string targetDirectory, out string realFilename, string defaultName = "unknown.txt") 
     { 
      // first set the filename to a non existing filename 
      realFilename = string.Empty; 
      bool succes = false; 

      try 
      { 
       HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
       request.Method = WebRequestMethods.Http.Get; 
       using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
       { 
        if (response.Headers.HasKeys()) 
        { 
         /* in case no header found that is called "Location" you get your default set filename as a fallback */ 
         realFilename = Path.Combine(targetDirectory, response.Headers["Location"] ?? defaultName); 
        } 
        else 
        { 
         realFilename = Path.Combine(targetDirectory, defaultName); 
        } 
        using (Stream responseStream = response.GetResponseStream()) 
        { 
         int blockSize = 8192; 
         byte[] buffer = new byte[blockSize]; 
         int result; 
         if (!Directory.Exists(targetDirectory)) 
         { 
          Directory.CreateDirectory(targetDirectory); 
         } 
         using (FileStream targetStream = new FileStream(realFilename, FileMode.Create, FileAccess.Write)) 
         { 
          do 
          { 
           result = responseStream.Read(buffer, 0, buffer.Length); 
           targetStream.Write(buffer, 0, result); 
          } while (result > 0); 
         } 
        } 
       } 
       succes = true; 
      } 
      catch (WebException wex) 
      { 
       if (wex.Response != null) 
       { 
        wex.Response.Close(); 
       } 
       Console.WriteLine("WebException occured: {0}", wex.Message); 
      } 
      catch (FileNotFoundException fnfe) 
      { 
       Console.WriteLine("FileNotFoundException occured: {0} not found! {1}", fnfe.FileName, fnfe.Message); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine("Error occured: {0}", ex.Message); 
      } 

      return succes; 
     } 

     static void Main(string[] args) 
     { 
      string filename; 
      if (DownloadFile("http://www.google.com", Environment.CurrentDirectory, out filename)) 
      { 
       Console.WriteLine("Saved file to {0}", filename); 
      } 
      else 
      { 
       Console.WriteLine("Couldn't download file!"); 
      } 
      Console.ReadLine(); 
     } 
    } 
} 
Смежные вопросы