0

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?

5
  • In your PHP script, which method are you using to deliver the file to the user? Commented Jul 21, 2012 at 18:29
  • is function taked from phpbb3. that is in file.php.you can view by find my other question asked today. Commented Jul 21, 2012 at 18:55
  • Do you have a working, online example of your PHP script? Commented Jul 21, 2012 at 19:03
  • yes,it is now settig to run only via my application. Commented Jul 21, 2012 at 19:10
  • ops,iv'e preess enter key :(( the correct code is [link]pastebin.com/QvjZM5b8 Commented Jul 21, 2012 at 19:13

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

18 Comments

there is process for direct download file,in our example i know filename and extension. i nedd haow to retrive it when data is generated in a php file.
@devilkkw please see my comment on your question.
Object reference not set to an instance of an object in line String header = client.ResponseHeaders["content-disposition"];
First: does your script contains content-disposition property in header?
I give up. Please ensure that you're actually creating a client, and making the download. Also use the debugger to check the values of the properties and variables to discover which object is null.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.