Monday, March 12, 2012

View Pdf files on the webpage

Hello,

Can someone show me how to view the pdf report on the web page? I kinda have an idea of how it goes but not not sure how to implement it. What I have right now is each user has a report folder from what they run a search from previous. I want a user to have an option to select which pdf report they would like to view in the folder when select that report's file. How do I do that? Thanks...

Just to clarify -

So the PDF's already exist, and you just want to be able to bring them up in a browser?


I can think of two ways to go about it, if I'm understanding you correctly.

1) The PDF already exists. User clicks on which PDF they want to open, then you redirect them a URL that displays the selected PDF. Nothing to hard.

redirect ="<script language='javascript'>window.open('PDF URL','Print') ;</script>";

Response.Write(redirect);

2) You're creating the PDF on the fly, and you want to give it to the HttpResponse for the user to see.

Response.ContentType ="application/pdf";Response.AddHeader("content-disposition","inline; filename=myPDF.PDF");// The next part I'm not sure about. I've seen it written to the Response// using BinaryWrite(). GL :)Response.BinaryWrite("your pdf byte stream");Response.End();

Thanks tysonh28 for replying...

Yes, the PDF files are alrealy exist in the user's folder,

What I want to do is connect to the user's report folder, count how many PDF files are in there and lists a report file's name.

When a user click on one those those report's name, It opens up and view as a PDF file on the web browser. How do I do that?


tysonh28:

I can think of two ways to go about it, if I'm understanding you correctly.

1) The PDF already exists. User clicks on which PDF they want to open, then you redirect them a URL that displays the selected PDF. Nothing to hard.

redirect ="<script language='javascript'>window.open('PDF URL','Print') ;</script>";

Response.Write(redirect);

Option one is what I want, the PDF file is already exists in the user's folder, How do I read the pdf file in the folder and direct them to view it? Can you be more specific for me please, thanks


I don't have much experience with displaying files to end-users over a web-app. Were it me, I'd create a webpage that would simply have a list of your pdf's for the user (user a GridView, Repeater, etc.). If I went with the GridView control then I would make a hyperlink to each report in the GridView. The user could then click whichever report (PDF) they wanted to view. From here it's a simple matter of using javascript to open a new window to the URL of the selected PDF.

<script language='javascript'>window.open('http://www.yourwebsite.com/userReports/SomeReport.pdf','Print') ;</script>

However, I'm sure there are dozens of ways to do this, and probably easier and better at that! GL!


Thanks Tysonh28 for all the information, really appreciate.

I could use the Gridview control and the hyperlink to link to those pdf files, but the thing is each time a user comes back and run a search report, it creates one pdf report file, then I have to go back to the code and add one more pdf links to the gridview each time a user run a report? This is a little too much, So is there a better way to read and show all the pdf files in the folder onto the gridview. I know in .mdb, you can select all from that database and it shows all files in that .mdb. Is there a better way to list all the pdf files in the folder on the Gridview and display it when click that link?


I think I lost you somewhere.

Assuming that you know where all of the PDF's are at (folder location), you can use the System.IO.DirectoryInfo class to get a list of all of the PDF's in that folder. From there it's just a matter of shoving that list into a DataSet, and then assigning that DataSet to the DataSource of your Gridview.

That was basically what I had in mind.


what you have to do is

- the folder contains these files must be in the web application virtual path.

- use the web application virtual path to open them

example http:\\www.example.com\ReportFolder\FileName.PDF



tysonh28:

I think I lost you somewhere.

Assuming that you know where all of the PDF's are at (folder location), you can use the System.IO.DirectoryInfo class to get a list of all of the PDF's in that folder. From there it's just a matter of shoving that list into a DataSet, and then assigning that DataSet to the DataSource of your Gridview.

That was basically what I had in mind.

What you are saying is exactly what I'm looking for. I got the connection part to the folder where all the PDF files resided, But not sure how to bind the pdf files in that folder to the Gridview, This is not the same as binding the .mdb to the Gridview, So I can't use the Oledbcommand or Oledbconnection for it. I try to bind the files to the Gridview by using the DataSet and assign that DataSet to the DataSource in the Gridview but gets the following errors,

So can you or anyone show me the right way to bind all pdf files to the gridview and view it on the web when click to that file? Thanks very much

protectedvoid Page_Load(object sender, System.EventArgs e)

{

string retVal;string username;

username = Session["user"].ToString();

retVal =ConfigurationManager.AppSettings.Get("RootDir") + username +ConfigurationManager.AppSettings.Get("ReportFolder");DataSet ds =newDataSet(retVal);

GridView1.DataSource = ds;

GridView1.DataBind();

}

I got this errorException Details:System.Web.HttpException: The IListSource does not contain any data sources.

Source Error:

Line 27: DataSet ds = new DataSet(retVal);Line 28: GridView1.DataSource = ds;Line 29: GridView1.DataBind();


Tamer Fathy:

what you have to do is

- the folder contains these files must be in the web application virtual path.

- use the web application virtual path to open them

example http:\\www.example.com\ReportFolder\FileName.PDF

Thanks Tamer for replying.

The folder contains these files are not in the web application virtual path, It resided in the server in local drive. D:\Winnt\Profiles\username\ReportsFolder\filename.pdf

Each user has their own Report's folder, they have to login with their username and password to see all their Report files. I already get the connection part to the user's report folder, I but not sure how to append those files to the Gridview or to the selection box, What I want is when a user click on one of the pdf file, it opens up for the pdf report, just like we double click on the pdf report to view it? any ideas how to approach that? thanks.


I get it now This is a piece of cake in ASP.NET since you can utlize the powerful
functionality of .net framework components. you can use the System.IO classes to get binary data from the
external file system (even on remote share folder) and write it out to
page's response stream. e.g.

You have tow approaches one is using binary which reads the file as binary and the other is text string

//binary approach

protected void BinaryWriteMethod()
{
byte[] bytes =null;
string path ="X:\\\\temp.txt";

bytes = System.IO.File.ReadAllBytes(path);

Response.ClearHeaders();
Response.ClearContent();
Response.ContentType ="text/plain";
Response.ContentEncoding = Encoding.UTF8;
Response.AddHeader("Content-disposition","attachment;
filename=sometext.txt");

Response.BinaryWrite(bytes);
Response.End();

}
//textstring approach

protected void StringWriteMethod()
{

string path ="X:\\test\\test.txt";

StreamReader sr =new StreamReader(path, Encoding.UTF8);

string data = sr.ReadToEnd();

sr.Close();

Response.ClearHeaders();
Response.ClearContent();
Response.ContentType ="text/plain";
Response.ContentEncoding = Encoding.UTF8;
Response.AddHeader("Content-disposition","attachment;
filename=sometext.txt");

Response.Write(data);
Response.End();
}


for string text approach, you need to make sure the text

encoding(specified in code) matches the text file's text encoding.

for more info check this post on Microsoft support http://support.microsoft.com/kb/307603


Thanks Tamer for replying and for the code, I get a little confuse on this, the code above is how do you cast it in the asp page? Can you be more specific on how to get files in the folder to listed on the aspx page and view it as pdf page? I want to have a list of all pdf files in the folder on the aspx page and when a user click one of that file's name, it opens up a pdf file. What I have right now is I did not add any pdf files in the project because files in the folder could be more than one in the future. So, I use the DirectoryInfo class to get the file's folder and use the GetFile to get all files in that folder, then bind it to the Gridview to give a list of file's link, when a user click on one of those link it open up a pdf report. The code below I have is just returning the pdf filename and not the pdf file link. I try to add the "ButtonField" in the Gridview and bind that field to the file's name, but it doesn't work. Do you know how to bind the ButtonField to the pdf file and view it when click on one of those link? In another word, how do I cast all pfd files in the folder on the aspx page and view it when click on one of those file's link?

Again thanks for your help :-)

protectedvoid Page_Load(object sender, System.EventArgs e)

{

string username;

username = Session["user"].ToString();

// Make a reference to a directory.

DirectoryInfo dir =newDirectoryInfo(ConfigurationManager.AppSettings.Get("RootDir") + username +ConfigurationManager.AppSettings.Get("PdfFiles"));

// Get a reference to each file in that directory.

FileInfo[] fileArr = dir.GetFiles();

GridView1.DataSource = fileArr;

GridView1.DataBind();

}

<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False">

<Columns>

<asp:ButtonFieldText="File's Name"CommandName="FileName"/>

</Columns>

</asp:GridView>


Boston,

this thread may be helpful to you.

http://forums.asp.net/t/1166280.aspx

He's loading up a dropdown with pdf's. That could be easier for you rather than using a GridView.


And just in case you haven't seen this one, they're loading up a GridView with PDF's from a folder - exactly what you wanted, right? =)

http://forums.asp.net/t/1162385.aspx

0 comments:

Post a Comment