2

I'm creating asp.net mvc 5 application.In that application I want generate a Folder once I click a button on front end view page.

I want to generate that folder with in following location ~/Essential_Folder/

<input type = "button" value="Create_Folder" class="btn btn-default" id="create_folder"/>

How can I do this ,

  1. can I do this using Server side language (in my case its C#), if its how ?

  2. is this possible to do using client side language (such as JavaScript) ?

script

<script type="text/javascript">

     $('btn-default').click(function () {


     });

  </script>
5
  • 3
    You need to handle the buttons .click() event and use ajax to call a controller method that creates the folder. Commented Nov 23, 2015 at 5:27
  • @StephenMuecke That's actually a server side. Because there is no way a javascript or jquery can actually create file/folder in windows due to security. So why not directly make button hit the controller action to create directory! Commented Nov 23, 2015 at 5:34
  • @dotnetkid, That exactly what my comment was stating Commented Nov 23, 2015 at 5:35
  • @StephenMuecke so I should look at "create a folder using ajax in asp.net mvc" Is it ? Commented Nov 23, 2015 at 5:37
  • The controller code just needs to use the System.IO.Directory.CreateDirectory() method (and return a JsonResult to indicate sucess or otherwise which you can test in the ajax success callback) Commented Nov 23, 2015 at 5:43

1 Answer 1

2

As @Stephen mentioned, you need to use ajax in order to create a folder. So you can have an action method like this:

    [HttpPost]
    public JsonResult CreateDirectory()
    {
        //if location has folder called "Essential_Folder" it should allow to goto inside of this if condition
        if (Directory.Exists(Server.MapPath("~/Content/Essential_Folder/")))
        {
            Directory.CreateDirectory(Server.MapPath(string.Format("~/Content/Essential_Folder/NewDir_{0}",
            DateTime.Now.Millisecond)));
            return Json("OK");
        }
        return Json("NO");
    }

And your ajax call should something like this:

<script type="text/javascript">

    $('.btn').click(function() {
        $.ajax({
            url: "@Url.Action("CreateDirectory")",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function (response) {
                alert(response.responseText);
            },
            success: function (response) {
                if (response === 'OK')
                    alert("Directory has been created");
                else
                    alert("errro");
            }
        });
    });

</script>
Sign up to request clarification or add additional context in comments.

6 Comments

Since the call making modifications, it probably should be a POST rather than a GET (would not want users to be able to navigate to it)
Don't use Server.MapPath twice for a same location.
@SirwanAfifi thanks I'm trying to integrate this solution , Here I have additional question , If we want to create new folder , should we create inside ~/Content location ?
It's common to store your files and folders inside ~/Content, but you can also use ~/Essential_Folder/
@SirwanAfifi But long time ago , in mvc 4 application I created folder manually inside Root location , then I deployed that project using IIS manager ,but then that folder disappeared , I thought that why you refer that location
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.