locked
select folder from local and show the full path in my text box RRS feed

  • Question

  • User202943253 posted

    Hello,

    I  am working with asp.net mvc (telerik ui)

    I have a textbox named path of poste.

    when entering a value, it should show me a window with tree view of my local directories.

    when I select a directory, I need to show only the full path in my text box.

    So i need to show the path of the selected folder from my local folders. How can I do this ?

    Any help or suggestions please ?

    Monday, December 21, 2020 4:50 PM

All replies

  • User475983607 posted

    Hello,

    I  am working with asp.net mvc (telerik ui)

    I have a textbox named path of poste.

    when entering a value, it should show me a window with tree view of my local directories.

    when I select a directory, I need to show only the full path in my text box.

    So i need to show the path of the selected folder from my local folders. How can I do this ?

    Any help or suggestions please ?

    This is a support forum.  The idea is you design and write code to meet your requirements.  If you run into trouble then share your code.  Explain how the code is intended to work and how the code actually works.  

    At this point you are asking the community design and write your code.  It also seems this question might be related to a Telerik control.  You should post this question on Telerik's support forum for help using the paid for framework.  

    Monday, December 21, 2020 4:55 PM
  • User-474980206 posted

    If it’s the local folder on the client machine and not the server, this capability does not exist. More secure browsers won’t even give the path to a selected file in the file control.

    Tuesday, December 22, 2020 12:43 AM
  • User202943253 posted

    so can I do this in the server side ? it's possible ? if yes ..how can i implement it ?

    Tuesday, December 22, 2020 1:36 PM
  • User475983607 posted

    so can I do this in the server side ? it's possible ? if yes ..how can i implement it ?

    You have not provided enough information or code to understand what you are trying to do. If you are trying to read the files in the client machine, then no, reading client files is not possible form a web server.

    If all you want to do is get a directory listing from the server then see the Syustem.IO namespace.

    https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.getfiles?view=net-5.0

    Again, share your code.

    Tuesday, December 22, 2020 2:01 PM
  • User202943253 posted

    I apologize because I was not too clear.

    I have a simple form with some textBox to fill:

    Poste Name  poste1
    Backup path                                                                                        C:/Users/myFolder
    Save

     myView.cshtml

    @using (Html.BeginForm("Poste_Add", "Postes", FormMethod.Post))
    
    {  
    
      @Html.AntiForgeryToken()
                        <div class="form-horizontal" id="newPoste">
    
                            <div class="form-group">
                                <div class="form-row">
                                    @Html.LabelFor(model => model.NamePoste, htmlAttributes: new { @class = "control-label col-md-4" })
                                    <div class="col-md-8">
                                        @Html.EditorFor(model => model.NamePoste, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.NamePoste, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>
    
    
    
                            <div class="form-group">
                                <div class="form-row">
                                    @Html.LabelFor(model => model.BackupPath, htmlAttributes: new { @class = "control-label col-md-4" })
                                    <div class="col-md-8">
    									@Html.EditorFor(model => model.BackupPath, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.BackupPath, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>
    
                        </div>
                        <br />
                        <div class="form-group">
                            <div class="form-row">
                                <div class="col-md-12" style="text-align: right;">
                                    @Html.ActionLink("Annuler", "PostesList", "Postes", null, new { @class = "mr-1 mb-1 btn btn-secondary" })
                                    <input type="submit" value="Save" class="mr-1 mb-1 btn btn-primary" />
                                </div>
                            </div>
                        </div>
    					
    					}



    PostesController.cs

    public async Task<ActionResult> Poste_Add(PosteModel model)
    {
    
    using (var client = new HttpClient(new LoggingHandler(new HttpClientHandler())))
    client.BaseAddress = new Uri(AppSetting.AppPath);
    var result = await client.PostAsJsonAsync("api/Postes", model);
    if (result.IsSuccessStatusCode)
    {
    TempData["NotificationMessage"] = new NotificationModel() { Severity = "success", Title = "added !" };
    return RedirectToAction("PostesList");
     }
     else
    {
    ModelState.AddModelError(string.Empty, "Server error try after some time.");
    }
     return View();
     }

     my WS works fine when adding data.

    I want to add for example a button  right next the backup path field.

    When i chick this buttonn i need to get the browse option so that i can browse all folders in my system, after that if i select the folder, the folder path should be displayed in my TextBox like C:/Users/myFolder/   in textbox.. also when select an UNC path and test if the folder exists in my local folders.

    this is what i'm looking for.. thanks !

    Tuesday, December 22, 2020 5:28 PM
  • User475983607 posted

    As explained several times, it is NOT possible to for a web server to save a file on a client machine.  The client must download the file from the server. 

    If the server has access to a network share, then it is up to you to write code that displays location options the user can select or provide a freeform input if the user knows what to enter.

    Tuesday, December 22, 2020 6:18 PM
  • User202943253 posted

    Sorry, but it seems you didn't understand my question.

    this is not what i am looking for because i don't want to save a file on a client machine.  I need just to get the path of the selected folder not file and save it as a string in my DB.


    Thank you.

    Tuesday, December 22, 2020 6:32 PM
  • User475983607 posted

    silbahi

    Sorry, but it seems you didn't understand my question.

    this is not what i am looking for because i don't want to save a file on a client machine.  I need just to get the path of the selected folder not file and save it as a string in my DB.

    I understand the question.  There is no tool that allows a browser to select a folder from a network share or a location on the web server. 

    It is up to you as the developer to write the UI.  You can use a text input where the user can enter a folder or you can provide a list of folders the user can select.  Either way it is up to you to design and write code.

    Tuesday, December 22, 2020 6:45 PM
  • User202943253 posted

    If I had found the way to do this, I would not have asked the question to help me.

    This is what i am looking for because i didn't fount the way to make the user enter  a folder in the text box as you explained.

    Best regards.

    Tuesday, December 22, 2020 7:05 PM
  • User753101303 posted

    Hi,

    Do you mean "local" to the browser side or "local" to the server side? If you want to uplaod files see for example How to upload files to ASP.NET MVC application (aurigma.com) .If you want to show a server side tree, there is not built in control for that and you'll have to render your own.

    Your exact intent could also help (what would you do with this path ?, keep in mind the web server doesn't have access to browser side files)

    Edit: this is to select in which server side folder you'll uplload a client side file?

    Edit 2: seems I missed your explanation. For now it seems your intent is to write content directly to a location on the browser side. It may appear to work when testing with the web server and the browser side being the same machine but a real web server doesn't have access to client side disk for safety reason.

    The usual approach for downloading a file is to trigger a download dialog in the browser using for example FileResult Class (System.Web.Mvc) | Microsoft Docs and the user then uses this dialog to do whatever he wants with this content.

    Tuesday, December 22, 2020 7:42 PM
  • User475983607 posted

    This is what i am looking for because i didn't fount the way to make the user enter  a folder in the text box as you explained.

    User input is covered in every beginning level MVC tutorial. 

    Below is a basic example that display a list of folder in a select.  These folder exist on the server.   I provided a link above that shows how to use the System.IO namespace to parse the file system.  There is also a text input which allows the user can select/create a sub folder if needed.  Helpfully this sparks some ideas on your end.

    namespace MvcBasic.Controllers
    {
        public class ViewModel
        {
            public List<SelectListItem> Options { get; set; }
            [Required]
            public string SelectedOption { get; set; }
            public string Folder { get; set; }
        }
        public class BasicFormsController : Controller
        {
            // GET: BasicForms
            [HttpGet]
            public ActionResult Index()
            {
                ViewModel vm = new ViewModel()
                {
                    Options = PopulateOptions()
                };
    
                return View(vm);
            }
    
            [HttpPost]
            public ActionResult Index(ViewModel model)
            {
                ViewModel vm = new ViewModel()
                {
                    Options = PopulateOptions(),
                    SelectedOption = model.SelectedOption
                };
                ViewBag.Message = $"You selected directory {model.SelectedOption} and folder {model.Folder}";
                return View(vm);
            }
    
            private List<SelectListItem> PopulateOptions()
            {
    
                string[] dirs = Directory.GetDirectories(@"c:\Projects", "*", SearchOption.TopDirectoryOnly);
                List<SelectListItem> options = new List<SelectListItem>();
                foreach (string dir in dirs)
                {
                    options.Add(new SelectListItem() 
                    {
                        Text = dir,
                        Value = dir
                    });
                }
                return options;
            }
        }
    }
    @model MvcBasic.Controllers.ViewModel
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    <form method="post">
        <div>
            @Html.DropDownListFor(m => m.SelectedOption, Model.Options, "--Select--")
            @Html.TextBoxFor(m => m.Folder)
        </div>
        <div>
            <input id="Submit1" type="submit" value="submit" />
        </div>
    </form>
    
    
    @if(!string.IsNullOrEmpty(ViewBag.Message)) {
        <div>
            @ViewBag.Message
        </div>
    }

    Tuesday, December 22, 2020 9:33 PM
  • User-474980206 posted

    Sorry, but it seems you didn't understand my question.

    this is not what i am looking for because i don't want to save a file on a client machine.  I need just to get the path of the selected folder not file and save it as a string in my DB.


    Thank you.

    you can not select a folder on the client machine. The server can do a dir of any network shares (this requires the app pool to have domain access) and display a list the user could select. See system.if for dir commands the server can do.

    Wednesday, December 23, 2020 5:25 AM