Solved

Download PDF and save it locally using C#

Posted on 2011-09-23
14
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This article focuses on how to remove password security from multiple PDF files by Adobe Acrobat program. Sometimes it is essential to access the stored data items and to print, edit as well as copy content from Portable Document Format files in abs…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this second video of the Xpdf series, we discuss and demonstrate the PDFimages utility, which, in a single command, is able to extract all the images from a PDF file and save each one in a separate image file (PBM, PPM, or JPG). Download and inst…
Sometimes we receive PDF files that are in the wrong orientation. They may be sideways or even upside down. This most commonly happens with scanned or faxed documents. It is possible to rotate the view of these PDFs with the free Adobe Reader produc…

690 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