[英]create a folder once a button click in asp.net mvc
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.我正在创建 asp.net mvc 5 应用程序。在该应用程序中,我想在单击前端视图页面上的按钮后生成一个文件夹。
I want to generate that folder with in following location ~/Essential_Folder/
我想在以下位置生成该文件夹
~/Essential_Folder/
<input type = "button" value="Create_Folder" class="btn btn-default" id="create_folder"/>
How can I do this ,我怎样才能做到这一点 ,
can I do this using Server side language (in my case its C#), if its how ?我可以使用服务器端语言(在我的情况下是 C#)来做到这一点,如果它如何?
is this possible to do using client side language (such as JavaScript) ?这可以使用客户端语言(例如 JavaScript)吗?
script脚本
<script type="text/javascript">
$('btn-default').click(function () {
});
</script>
As @Stephen mentioned, you need to use ajax in order to create a folder.正如@Stephen 提到的,您需要使用 ajax 才能创建文件夹。 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:你的 ajax 调用应该是这样的:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.