Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

Download PDF and save it locally using C#

I want to download a file from a website or a shared drive to a local drive like opening an excel file and save it as excel file. In the given sample code, I am able to download xls file with this simple code.

Now I want to similar technique to download pdf or doc file.
Microsoft.Office.Interop.Excel.Application XL_App = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook XL_Book;


            XL_Book=XL_App.Workbooks.Open("http://alinkpdf.xls");
            XL_Book.SaveAs("C:\\data\\alinkpdf.xls");
            
            XL_App.Quit();

Open in new window

0
Efremdawit
Asked:
Efremdawit
  • 7
  • 6
1 Solution
 
Ramkisan JagtapLead DeveloperCommented:
You can download pdf files using below code.

   Response.ContentType = "Application/pdf";
  //Get the physical path to the file.
  string FilePath = "your files physical Path";
  //Write the file directly to the HTTP content output stream.
  Response.WriteFile(FilePath);
  Response.End();



Please refer Following Links
http://aspalliance.com/259_downloading_files__forcing_the_file_download_dialog
http://www.accuwebhosting.com/Articles/HOW_TO_DOWNLOAD_FILE_USING_ASP.NET.html

regards,
Ram
0
 
jonnidipCommented:
You don't need to create an object of the needed type.
You may just download the file you want:
using (System.Net.WebClient client = new System.Net.WebClient())
{
    client.DownloadFile(new Uri(@"http://downloads.sourceforge.net/filezilla/FileZilla_3.5.1_win32-setup.exe"), @"d:\downloaded.exe");
}

Open in new window


Regards.
0
 
EfremdawitAuthor Commented:
Thank you so much.

However you answer is for web based application. I need winform application (Stand alone application).

Would you please provide me a code for such task?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
jonnidipCommented:
Please try my solution. I use it in a WinForm application.
0
 
EfremdawitAuthor Commented:
Thank you so much. It worked for PDF files however did not work for excel file.
Why for curiosity purpose.
 
0
 
jonnidipCommented:
It should work for any kind of file, since it is just a downloader.
What kind of error message do you get?
0
 
EfremdawitAuthor Commented:
401 Unauthorized. It is related to credentials.
0
 
jonnidipCommented:
Before calling the "DownloadFile" method you can set the Credentials to the WebClient object.
For example, you can set it to:
client.Credentials = CredentialCache.DefaultCredentials;

...anyway, this problem should happen with your method too...
0
 
EfremdawitAuthor Commented:
I tried before with the following code.

For excel file, I have not had any problem.


private void btnDownload_Click(object sender, EventArgs e)
        {

            string url

               = @"http://test/test.xls";



            // Create an instance of WebClient

            WebClient client = new WebClient();

            WebClient webClient = new WebClient();

            webClient.Credentials = new System.Net.NetworkCredential("abcd", "1234", "ACD");
            webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);

            // Hookup DownloadFileCompleted Event

            client.DownloadFileCompleted +=

               new AsyncCompletedEventHandler(client_DownloadFileCompleted);


            // Start the download and copy the file to c:\temp
            client.DownloadFile("http://test/test.xls", "newtest.xls");
            //client.DownloadFileAsync(new Uri(url), @"c:\data\newtest.xls");

            
        }


        void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {

            MessageBox.Show("File downloaded");

        }

      
       
    }

Open in new window

0
 
jonnidipCommented:
It seems correct. You need to put your username, password and domain into the NetworkCredential object.
0
 
EfremdawitAuthor Commented:
it gave the error 401 Unauthorized with the above code. Why?

That is why I tried to open excel file and save the file rather than download the file with webclient.

So would you pls find the solution?
0
 
jonnidipCommented:
It really depends on your network authorization type.
When you download the file by browser, do you need to put a password?
Is that username/password/(domain) that you put in the credentials? The domain may be not needed, as the authentication type can be different.

This should be checked with your network administrator, it is not a programming issue.

Regards.
0
 
EfremdawitAuthor Commented:
When I am in the system (network of the site), I can download the file. I want to pass my credential when I am out of the site (network). I could not do it that. I put the username, password and domain as you can see from the above code.

Can you please help in regard??/
0
 
jonnidipCommented:
As said before, the logon type should be checked with your network administrator.
When in the same network the rules are different than over the Internet.

Is your initial question solved?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now