devilkkw
devilkkw

Reputation: 438

retriving php variable in c#

I have a PHP script which redirects the user to a file download. Upon viewing this page in a web browser I am automatically prompted for a location to save the file, with the correct filename and extension inside the SaveFileDialog.

I wish to download this file using an application written in C#. How can I retrieve the filename and extension of the file that is included in the response from the PHP script?

I think have to read the PHP variable, but I have not found the correct method to read it. The PHP variables in which I am storing the filename and extension are $file and $ext respectively.

I've read several questions here, but I'm confused. Some user speak about WebClient, others speak about HttpWebRequest.

Can you point me in the correct direction?

Upvotes: 0

Views: 1444

Answers (1)

seeker
seeker

Reputation: 3333

Take a look here, where the process of downloading and saving file is described.

Here's how to get file name from the request response headers:

String header = client.ResponseHeaders["content-disposition"];
String filename = new ContentDisposition(header).FileName;

And one more notice: here client is WebClient component. And here is how to use download with WebClient: enter link description here

------The full solution ----------------------------

As it turned out, your server uses authentication. That's why in order to download file we have to pass authentication. So, PLEASE write full details. And here's the code:

 private class CWebClient : WebClient
    {
        public CWebClient()
            : this(new CookieContainer())
        { }
        public CWebClient(CookieContainer c)
        {
            this.CookieContainer = c;
        }
        public CookieContainer CookieContainer { get; set; }

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = this.CookieContainer;
            }
            return request;
        }
    }
    static void Main(string[] args)
    {
        var client = new CWebClient();
        client.BaseAddress = @"http://forum.tractor-italia.net/";
        var loginData = new NameValueCollection();
        loginData.Add("username", "demodemo");
        loginData.Add("password", "demodemo");
        loginData.Add("login","Login");
        loginData.Add("redirect", "download/myfile.php?id=1622");
        client.UploadValues("ucp.php?mode=login", null, loginData);

        string remoteUri = "http://forum.tractor-italia.net/download/myfile.php?id=1622";
        client.OpenRead(remoteUri);
        string fileName = String.Empty;
        string contentDisposition = client.ResponseHeaders["content-disposition"];
        if (!string.IsNullOrEmpty(contentDisposition))
    {
        string lookFor = @"=";
        int index = contentDisposition.IndexOf(lookFor, 0);
        if (index >= 0)
            fileName = contentDisposition.Substring(index + lookFor.Length+7);
    }//attachment; filename*=UTF-8''JohnDeere6800.zip

       client.DownloadFile(remoteUri, fileName);


    }

On my PC that works.

Upvotes: 1

Related Questions