如何使用asp.net在浏览器中打开本地文件。 [英] How to open local files in browser using asp.net.

查看:462
本文介绍了如何使用asp.net在浏览器中打开本地文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,



我在文件夹里面的d:驱动器中有几种文件(pdf,html,png)。我想在浏览器上使用锚标记或任何超链接控件打开这些文件,但是网址不应该像d:// Somefolder / File那样显示它应该像Localhost:1234 / Somefolder / file。



我尝试了什么



我读取所有文件路径并将其绑定在网格中。现在在网格列上,我已经使用带有锚标记的模板字段。它显示文件路径..但​​是点击它之后,它就像d:/ somefolder / file一样打开。它应该像Localhost:1234 / Somefolder / file。怎么做到这一点。





以下是我的完整代码:



设计规范: -

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:-

<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>

< br $>










以下是我的代码背后的代码: -









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();
        }





先谢谢

Mukesh



Thanks in Advance
Mukesh

推荐答案




据我所知,从本地驱动器浏览器中无法查看文件,为此需要上传所有文件并保存在服务器(iis)中,你可以在网格视图中获取路径和绑定,但如果你点击你可以查看文件,如果你有在线查看器它看起来要查看,其他明智的文件内容出现在浏览器中,如果一行(.txt文件),然后行将出现在浏览器的左上角,你无法控制它(在浏览器中心查看)。





创建一个根文件夹下的文件夹(网站文件夹)添加该文件夹中的所有文件并在网格视图中绑定这样的路径,因此当打开文件路径时看起来像

Localhost:1234 / website / temp / resume.docx < br $>


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

~/Somefolder/resume.docx







注意:你可以看到这样的路径d:// Somefolder / File可以在同一个mac中查看文件,bcz客户端和服务器hine,使用ipaddres并运行其他系统u'tant查看文件。

问候

Aravind




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


这篇关于如何使用asp.net在浏览器中打开本地文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆