从带有 php 标头位置的 URL 获取文件名

本文关键字:URL 获取 文件名 位置 php | 更新日期: 2023-09-27 18:31:52

我有一个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# 中获取文件名?此文件仅供测试。我将从更新服务器发送软件更新。

我需要在 c# 中为此命名:

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

从带有 php 标头位置的 URL 获取文件名

您可以在下载文件时简单地检查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();
        }
    }
}