使用php标头位置从URL获取文件名

时间:2014-11-25 20:15:11

标签: c#

我在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);

1 个答案:

答案 0 :(得分:0)

您可以在下载文件时查看HttpWebResponse.Headers,它们应该包含您发送的标题。

我想你可以使用的一个例子是这个,我根据你的特定用途调整了一下(nl:你需要得到的文件名)

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();
        }
    }
}