Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
Hi Experts,

I have several kinds of file(pdf,html,png) in my d: drive inside a folder . I want to open these files on the browser using anchor tag or any hyperlink control but the url should not display the path like d://Somefolder/File it should be like Localhost:1234/Somefolder/file.

What I tried

I read all the file path and binded it in a grid. Now on the grid column I have taken Template field with anchor tag. It is displaying the file path..but after clicking on it, it is opening like d:/somefolder/file. it should be like Localhost:1234/Somefolder/file. How to achieve this.


Below is my complete code:

Code for Design:-
ASP.NET
<div>
         <label id="lblReadFile">Read File </label>

  <asp:GridView ID="gvFile" runat="server" AutoGenerateColumns="false">

      <Columns>
          <asp:BoundField HeaderText="ID" DataField="ID" />
          <asp:TemplateField HeaderText="File">
             <ItemTemplate>

                   <a target="_blank" href='<%# Eval("Path") %>'><%# Eval("Path") %></a>
              </ItemTemplate>
           </asp:TemplateField>
      </Columns>
  </asp:GridView><pre>
   </div>






Below is my code behind code:-

<pre lang="c#">
 public class FileData
        {
            public int ID { get; set; }
            public string Path { get; set; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
           
            int uid = 0;
            string filepathfromdir = "";
            List<FileData> files = new List<FileData>();
            
            foreach (var path in Directory.GetFiles(@"C:\Avishek\Test\"))
            {
                uid = uid+1;
                filepathfromdir = path.ToString();
               
                FileData fd = new FileData();
                fd.ID = uid;
                fd.Path = filepathfromdir ;
                files.Add(fd);

            }
            
            gvFile.DataSource = files.ToList();
            gvFile.DataBind();
        }


Thanks in Advance
Mukesh
Posted
Updated 5-Aug-15 22:15pm
v3
Comments
F-ES Sitecore 6-Aug-15 4:20am    
Are these files on the client or the server?
Mukesh Pr@sad 6-Aug-15 5:00am    
All these files on the server..

1 solution

Hi
In my knowledge from local drive u cant view file in browser,for this u need to upload all file and save in server (iis),from that u can get path and bind in grid view,but if u click u can view file ,if u have on-line viewer it look to view,other wise content of file appear in browser,if one line(.txt file) ,then line will appear in at top left of the browser,u cant control it(view in centre of browser).


Create one folder under root folder(website folder)add all files in that folder and bind path like this in grid view,so when open file path look like
Localhost:1234/website/temp/resume.docx

C#
~/Somefolder/resume.docx



Note: u can see path like this d://Somefolder/File can view file,bcz client and sever in same machine,using ipaddres and run form other system u cant view file.
Regards
Aravind
 
Share this answer
 
v4

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900