Solved

Download PDF and save it locally using C#

Posted on 2011-09-23
14
294 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now