上传文件 asp.net mvc 2

发布于 2024-11-05 09:21:41 字数 1594 浏览 0 评论 0原文

我正在尝试在上传文件后执行任务。任务完成后我想显示一些信息。目前,我有一个上传操作,该操作将在单击“执行任务”按钮后触发,这不太好。问题:我想只触发“Sometask”操作而不是上传操作?

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.BeginForm("Upload","Home",FormMethod.Post,new { enctype = "multipart/form-data" }) %>
<%{ %>
<%=Html.HiddenFor(model=>model.Filepath) %>
<input type="file" id="upload" name="upload" />
<button id="btnUpload">
    upload</button>
<%} %>
<button id="btnTask">
    Do Task</button>
<script type="text/javascript">
    $(document).ready(function (event) {
        $('#btnTask').click(function () {
            $.post("/Home/Sometask",
            { filePath: $("#Filepath").val() },
             function (data) {
                alert(data);
            });
            event.preventDefault;
        });
    });

</script>

    [HttpPost]
    public ActionResult Upload()
    {
        HttpPostedFileBase selectedFile = Request.Files["upload"];

        if (selectedFile.ContentLength > 0)
        {
            string filePath = Path.Combine(HttpContext.Server.MapPath("\\Uploads\\")
            , Path.GetFileName(selectedFile.FileName));
            selectedFile.SaveAs(filePath);
            UploadModel model = new UploadModel();
            model.Filepath = filePath;
            return View("Index", model);
        }

        return View("Index");
    }

    public string Sometask(string Filepath)
    {
        Thread.Sleep(5000);
        return "ready";
    }

i am trying to execute a task after uploading a file. After the task is finished i would like to display some info. At the moment i have an Upload action that will fire after clicking the 'Do task' button which is not good. question :I would like to just trigger the 'Sometask' action and not the Uploadaction?

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.BeginForm("Upload","Home",FormMethod.Post,new { enctype = "multipart/form-data" }) %>
<%{ %>
<%=Html.HiddenFor(model=>model.Filepath) %>
<input type="file" id="upload" name="upload" />
<button id="btnUpload">
    upload</button>
<%} %>
<button id="btnTask">
    Do Task</button>
<script type="text/javascript">
    $(document).ready(function (event) {
        $('#btnTask').click(function () {
            $.post("/Home/Sometask",
            { filePath: $("#Filepath").val() },
             function (data) {
                alert(data);
            });
            event.preventDefault;
        });
    });

</script>

    [HttpPost]
    public ActionResult Upload()
    {
        HttpPostedFileBase selectedFile = Request.Files["upload"];

        if (selectedFile.ContentLength > 0)
        {
            string filePath = Path.Combine(HttpContext.Server.MapPath("\\Uploads\\")
            , Path.GetFileName(selectedFile.FileName));
            selectedFile.SaveAs(filePath);
            UploadModel model = new UploadModel();
            model.Filepath = filePath;
            return View("Index", model);
        }

        return View("Index");
    }

    public string Sometask(string Filepath)
    {
        Thread.Sleep(5000);
        return "ready";
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月如刀 2024-11-12 09:21:41

是否调用了 Upload() 方法?查看代码,我希望调用 Sometask() 而不是 Upload() 方法。单击按钮时,jQuery 代码将调用 .post,这应该消除正常的表单发布。如果 Sometask() 没有被调用,您可能需要向 Sometask() 方法添加 [HttpPost] 属性。

请注意,出于安全原因,无法从 Javascript 上传文件。

Is the Upload() method being called? Looking at the code, I would expect the Sometask() to be called but not the Upload() method. The jQuery code is calling .post when the button is clicked and that should eliminate the normal form posting. If Sometask() is not getting called, you many need to add [HttpPost] attribute to the Sometask() method.

Note that for security reasons files cannot be uploaded from Javascript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文