Solved

Download PDF and save it locally using C#

Posted on 2011-09-23
14
304 Views
Last Modified: 2012-05-12
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
Comment
Question by:Efremdawit
  • 7
  • 6
14 Comments
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36591950
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
 
LVL 13

Expert Comment

by:jonnidip
ID: 36591970
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
 

Author Comment

by:Efremdawit
ID: 36594982
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 13

Expert Comment

by:jonnidip
ID: 36595440
Please try my solution. I use it in a WinForm application.
0
 

Author Comment

by:Efremdawit
ID: 36707709
Thank you so much. It worked for PDF files however did not work for excel file.
Why for curiosity purpose.
 
0
 
LVL 13

Expert Comment

by:jonnidip
ID: 36707742
It should work for any kind of file, since it is just a downloader.
What kind of error message do you get?
0
 

Author Comment

by:Efremdawit
ID: 36709946
401 Unauthorized. It is related to credentials.
0
 
LVL 13

Expert Comment

by:jonnidip
ID: 36711097
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
 

Author Comment

by:Efremdawit
ID: 36711394
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
 
LVL 13

Accepted Solution

by:
jonnidip earned 125 total points
ID: 36711433
It seems correct. You need to put your username, password and domain into the NetworkCredential object.
0
 

Author Comment

by:Efremdawit
ID: 36711812
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
 
LVL 13

Expert Comment

by:jonnidip
ID: 36711873
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
 

Author Comment

by:Efremdawit
ID: 36712678
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
 
LVL 13

Expert Comment

by:jonnidip
ID: 36713288
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever come up with a need of emailing only few pages of PDF file to one of yourfriend or colleague, instead of whole Adobe file? If yes, then surely you have face problems in doing that! Read this section as I have suggested multiple solutio…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this first video of the three-part Xpdf series, we introduce and describe Xpdf, a library containing nine command line utilities that perform various functions on PDF files. We show where the library is located and how to download it, discuss its…
We often encounter PDF files that are pure images, that is, they do not have text characters, but instead contain only raster graphics. The most common causes of this are document scanning software and faxing software/services that create image-only…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question