locked
how to open file from server by link RRS feed

  • Question

  • User-1634604574 posted

    i have mutiple file inside server with all type extension i want from the view create a link for each of that files and by clicking on that each link open the file

    Tuesday, January 29, 2019 10:50 AM

Answers

  • User1520731567 posted

    Hi zhyanadil.it@gmail.com,

    i have mutiple file inside server with all type extension i want from the view create a link for each of that files and by clicking on that each link open the file

    According to your requirement,I suggest you could use Directory.GetFiles to get all files by specific path and then handler data ,pass them to ViewBag,display by foreach in view.

    For example:

    controller:

                var path = Server.MapPath(@"~/images/");            
    List<string> fileList=new List<string>(); string[] picList = Directory.GetFiles(path);//get all files from images folder foreach (string f in picList) { string fName = f.Substring(path.Length); fileList.Add(fName); } ViewBag.fileList = fileList;

    View:

    @foreach (var item in ViewBag.fileList)
    {
        <br />
        <a href="@Url.Content("~/images/"+item)">@item</a>
    }
    

    How it works:

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 30, 2019 5:42 AM

All replies

  • User197322208 posted

    1 . Obtain the list of files names

    2. Pass this list to the View as a Model from the Action of the Controller

    3. Render in the view as <A href=...

    ( this approach does not take into discussion the security) 

    Tuesday, January 29, 2019 12:01 PM
  • User1520731567 posted

    Hi zhyanadil.it@gmail.com,

    i have mutiple file inside server with all type extension i want from the view create a link for each of that files and by clicking on that each link open the file

    According to your requirement,I suggest you could use Directory.GetFiles to get all files by specific path and then handler data ,pass them to ViewBag,display by foreach in view.

    For example:

    controller:

                var path = Server.MapPath(@"~/images/");            
    List<string> fileList=new List<string>(); string[] picList = Directory.GetFiles(path);//get all files from images folder foreach (string f in picList) { string fName = f.Substring(path.Length); fileList.Add(fName); } ViewBag.fileList = fileList;

    View:

    @foreach (var item in ViewBag.fileList)
    {
        <br />
        <a href="@Url.Content("~/images/"+item)">@item</a>
    }
    

    How it works:

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 30, 2019 5:42 AM