views:

148

answers:

3

Hi everyone,

I'm confused with the code block below because why it downloads the contents of the webpage rather the file itself. I create dummy file with some texts in it then I download it, but when I open the download file, I don't see any text that I wrote but it has weird web language tags.

    private bool DownloadCSVfile()
    {
        bool downloadOk = false;
        WebClient client = null;

        try
        {
            client = new WebClient();
            client.Credentials = CredentialCache.DefaultCredentials;
            client.DownloadFile(myURL, CSVfile);

            if (File.Exists(CSVfile))
                downloadOk = true;
            else
                downloadOk = false;
        }
        catch (Exception error)
        {
            downloadOk = false;
            string err = error.Message;
        }

        //release resource
        if (client != null)
        {
            client.Dispose();
            client = null;
        }

        //
        if (downloadOk == true)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
+1  A: 

I'm guessing myURL ends in "/" and not ".csv" Am I right? Are you thinking that myURL + CSVFile will contain the full path to the file? It won't. (See doc) The URL has to be the path to the file itself. You are allowed to have something like this:

client.DownloadFile(@"./file.csv", "filename.txt");

If I'm not on the right track, please tell what's in the URL and what some of the first several tags are in the downloaded file.

JeffH
yep myURL ends with "/", and file name is in the second parameter of the DownloadFile().
Bopha
Add the file name after the '/' in myURL.
JeffH
here are the tags in the download file:<!-- _lcid="1033" _version="11.0.8161" _dal="1" --><!-- _LocalBinding --><html dir="ltr"><HEAD> <META Name="GENERATOR" Content="Microsoft SharePoint"> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8"> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
Bopha
really, I have to add my filename after "/", so would be like this: client.DownloadFile(myURL + CSVfile, CSVfile);
Bopha
Yes. The first argument has to specify the location of the file, not just the directory that the file is in, as you have it in your question.
JeffH
JeffH, it works thanks. Thanks alot.
Bopha
A: 

Does the site require a login and it's really redirecting you?

I've had one before that required me faking my browsing movements (and storing cookies and such) before it would allow me to download a file I needed.

Nazadus
Yep the site requires for login. So after I login with my ID and password then I just upload the file manually and run the code. I also try deleting the file, then run the code, but still it downloads the webpage contents.
Bopha
So do you have any suggestion for me to fix this?
Bopha
Please provide more info as to what myURL is pointing to, and what "webpage content" you are actually receiving.
the.jxc
A: 

Sorry, what does "the webpage" mean in "the contents of the webpage". There's only one URL involved here, which is myURL. Does myURL point to the CSV file directly? If so, then what does the contents that you are receiving actually look like?

Also:

if (File.Exists(CSVfile))
    downloadOk = true;
else
    downloadOk = false;

...is embarrassing. Please write:

downloadOk = File.Exists(CSVfile);

That has identical results, in 1 line instead of 4.

//
if (downloadOk == true)
{
    return true;
}
else
{
    return false;
}

That is even worse. The single line:

return downloadOk;

...does exactly the same thing in 1 line of code instead of 7.

Also, both lines in your exception block...

catch (Exception error)
{
    downloadOk = false;
    string err = error.Message;
}

...do absolutely nothing. downloadOk will always be false in your exception block, and the local variable "err" is never used before it goes out of scope.

the.jxc
thanks for pointing that out, but I want to be clear and easy to read.
Bopha
after I download the file, and I open the download file and I see bunch of webpage tags, so maybe it is not the actual webpage content, but it's bunch of tags which I don't understand. It likes html tags or something. And yep myURL actually points to where my CSV file it. for example, DownloadFile(@"http://myURL/", "dummy.csv")
Bopha
DownloadFile("http://myURL/", "dummy.csv") with @ beginning of "http:...
Bopha
local variable err, I just use it to see the type of error message when I debug through it because I don't want to show the exception error in message box.
Bopha