Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
I have already upload pdf files from browser and I have successfully shown the files in iframe while I am uploading pdf files and those pdf files are saving in a folder named journal and this folder is exist in my application start-up path.
To do that my code is here:
private void WriteToFile(string strPath, ref byte[] Buffer, string filename)
        {
            System.IO.FileStream newFile = new System.IO.FileStream(strPath, System.IO.FileMode.Create);
            newFile.Write(Buffer, 0, Buffer.Length);
            newFile.Close();
            myframe.Attributes["src"] = filename;
 
        }
 protected void Button1_Click1(object sender, EventArgs e)
        {
            try
            {
                lblErr.Visible = false;
                HttpPostedFile MYFILE = FileUpload1.PostedFile;
 
                byte[] myData = new byte[MYFILE.ContentLength];
                MYFILE.InputStream.Read(myData, 0, MYFILE.ContentLength);
                string strFilename = System.IO.Path.GetFileName(MYFILE.FileName);
WriteToFile(Server.MapPath("~/Journal/" + strFilename), ref myData, "Journal/" + strFilename);
 
}
            catch (Exception ex)
            {
                lblErr.Text = ex.Message.ToString();
                lblErr.ForeColor = Color.Red;
                lblErr.Visible = true;
            }
        }
And My XHTML code is here:
<div style="width:100%; height:711px">
<asp:FileUpload ID="FileUpload1" runat="server" 
                        style="left: 12px; position: absolute; height: 21px; width: 217px; top: 156px" />
<asp:Button ID="Button1" runat="server" 
                        style="position: absolute; height: 26px; width: 192px; top: 153px; right: 308px" 
                        Text="Upload Research Paper" onclick="Button1_Click1" />
<iframe id="myframe" width="100%" height="100%"  runat="server"></iframe>
</div>
In this way I am uploading pdf files and showing them in iframe. But, now I have to get these existing pdf files Location information to link in a Hyperlink label to show those pdf files again in iframe when I will click in the Hyperlink label. So, I need yours help now to do so.
Thank you..
Posted 29-Jul-12 8:26am
Edited 29-Jul-12 9:09am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You will probably need to do something like this...
 
System.IO.FileInfo[] finfos = (new System.IO.DirectoryInfo(Server.MapPath("~/Journal")).GetFiles("*.pdf"));
 
           foreach (System.IO.FileInfo finfo in finfos)
           {
               //create link label
           }
 
However depending on how many different files you might end up with you might want to think about tracking each uploaded file in either a database or XML file.
Then you have the option of storing name, description, uploaded date, where it was uploaded from etc.
  Permalink  
Comments
UL UL ALBAB at 30-Jul-12 15:19pm
   
No. I want as like this:
string filePath = Server.MapPath("admit.pdf");
myframe.Attributes.Add("src", "" + filePath.ToString());
 


Error :
Firefox doesn't know how to open this address, because the protocol(c) isn't associated with any program.

Please tell me how to solve this problem to show the pdf file in IFrame ?
Trak4Net at 31-Jul-12 2:03am
   
The url cannot be to the path local to the web server. You would either need to store the pdf's in a subdirectory that is available so the url would be something like "http://my.hosted.com/Journal/admit.pdf" otherwise you will need to create a page that will read the file and write the file contents to the response stream so you would end up setting your url to something that has the file name or id in the query "http://my.hosted.com/viewJournal.aspx?fid=admit.pdf"
For the later do a google on something like this "Write pdf to response stream"
Ananthikasivel at 7-Aug-12 9:29am
   
Any other solution to display pdf file in iframe with absolute path?
Member 8533630 at 11-Jun-13 2:10am
   
I am also facing this same problem .please give any help regarding this article.
Trak4Net at 30-Jul-13 20:21pm
   
If you are pointing to a local file such as a help file that is accessible from the iframe you may need to use the "file" protocol instead of "http" so the using "UL UL ALBAB"'s example it would be something like this
 
string filePath = Server.MapPath("admit.pdf");
myframe.Attributes.Add("src", "file:///" + filePath.ToString());
 
note: I did not test this and you may need to deal with converting backslash (\) to forward slash(/) of filePath.
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 29 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100