Solved

Download PDF and save it locally using C#

Posted on 2011-09-23
14
301 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Video Player 11 23
Hey Audio! 3 30
List<PaisEntity> - show some 1 24
c# - Best approach for objects in functions 3 16
PDF files have been in the limelight due to its unmatched features.  Personal documents, emails, business reports and eBooks are all converted into PDF files owing to peerless features provided by it. Adding watermark to a PDF file is a method to se…
Inserting page numbers in Portable Document Files not only enhances manageability but also makes them look professional. With numbered pages, the file appears more organized and it becomes easier to search for a particular page. The size and the vol…
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…
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…

810 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