Solved

read pdf files

Posted on 2009-05-06
10
320 Views
Last Modified: 2012-05-06
I want to create a page that Accessing Info hyperlink to when user cicks that it will go search to get the latest dated pdf file and opens up for user to view. It should always search for the latest updated file. Do I need to use File Class or FileINfoClass for that? anyone can help me to set up that please?
0
Comment
Question by:1jaws
  • 7
  • 3
10 Comments
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24319203
Dim dir As New System.IO.DirectoryInfo("Path")
        For Each file As System.IO.FileInfo In dir.GetFiles()
            ' Process each file here
            file.LastWriteTime -> Use this property
        Next

Use System.IO.StreamReader to read the file

the following to push the file to the browser
Response.AddHeader("content-disposition", "inline;")
            Response.AddHeader("Content-Type", "application/pdf")
            Response.BinaryWrite(pdf)  'pdf is a byte array
            Response.Flush()
            Response.Close()
            HttpContext.Current.ApplicationInstance.CompleteRequest()
0
 

Author Comment

by:1jaws
ID: 24319398
can I have this in C#? and I didnt understand what I need to do here   file.LastWriteTime -> Use this property
 can you be able to give example ..
lets say I have a pdf files in folder MY PDF and lets I have two files  
test1.pdf (updated  5/4/09)
test2.pdf (updated  5/5/09)
which means are I need to get the test2.pdf file because that is the latest updated pdf. So, this point how can I generate my code that user will click that Accessing Info link and see that pdf file test2?
0
 
LVL 15

Accepted Solution

by:
mohan_sekar earned 500 total points
ID: 24319654

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("c:\\myfolder");
    foreach (System.IO.FileInfo file in dir.GetFiles("*.pdf").OrderByDescending((System.Object x) => x.LastWriteTime)) {
        //Get the first file and exit
        processFile(file.FullName, file.Length);
        break; // TODO: might not be correct. Was : Exit For
    }

private void processFile(string fileName, Int64 fileLength)
{
    System.IO.BinaryReader s = new System.IO.BinaryReader(new System.IO.FileStream(fileName, IO.FileMode.Open));
    byte[] buf = null;
    s.Read(buf, 0, fileLength - 1);
    s.Close();
   
    Response.AddHeader("Content-Type", "application/pdf");
    Response.BinaryWrite(buf);
    //pdf is a byte array
    Response.Flush();
    Response.Close();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}

NOTE: Above code is not tested. It will compile, though.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:1jaws
ID: 24323026
thank you very much for showing in example, but for some reason I am still really not too clear about it. first part of code do I need to put it on page load  or the hyperlink clcik event I guess right? and do I need to specify (file.FullName,file.Lenght) part ..
sorry about that..
0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24324288
replace c:\myfolder with your forlder name. That's all. When do you want to show the PDF? If it's the on click event of the hyperlink, then copy the 1st part of the code to the onclick event.
0
 

Author Comment

by:1jaws
ID: 24361091
yes, I need hyperlink click event but hyperlink click event is different than regular click event  like for the button for ex.  how can I do that?
0
 

Author Comment

by:1jaws
ID: 24361142
I used linkbutton , for it..

I tested the code , it is giving this problem
'System.Array' does not contain a definition for 'OrderByDescending' and no extension method 'OrderByDescending' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)      

'object' does not contain a definition for 'LastWriteTime' and no extension method 'LastWriteTime' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)      
0
 

Author Comment

by:1jaws
ID: 24361170
also these
The best overloaded method match for 'System.IO.BinaryReader.Read(byte[], int, int)' has some invalid arguments

complaining about file  filelenght, cannot convert long to int -  do I need to put number that like 386 KB for ex.
      
0
 

Author Comment

by:1jaws
ID: 24367078
can anybody please help!
0
 

Author Closing Comment

by:1jaws
ID: 31578698
thank you!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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