12

I am using ASP .Net MVC 4.0 and VS10. I am a newbie in web application.

I have designed a page with html razor view. Here is some code of Index.cshtml:

@{
ViewBag.Title = "BAP Automation";
}
@section featured {
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>@ViewBag.Title.</h1>
                <h2>@ViewBag.Message</h2>
            </hgroup>
            <form action="Index">
            <table>              **//EDITED BELLOW**
                <tr><form action="" method="post">
                    <td>Upload Excel File: </td>
                    <td><input type="text" name="NAMEtxtFileName"/></td>
                    <td><input type="button" value="Upload" id="IDbtnUpload" name="NAMEbtnUpload"/></td>
                    </form>
                </tr>
                <tr>
                    <td>Company Name: </td>
                    <td><input type="text" /></td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td align="right"><input type="submit" value="Process" /></td>
                    <td></td>
                </tr>
            </table>
            </form>
        </div>
    </section>
}

I am trying to upload an excel file in NAMEbtnUpload's click event. clicking on this button we will be in this page, just a file upload dialog will open and selecting the file, the file location will be shown in the NAMEtxtFileName textbox.

EDIT 1:

I have written some code from the suggested code:

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase NAMEbtnUpload)
    {
        if (NAMEbtnUpload.ContentLength > 0)
        {
            var fileName = Path.GetFileName(NAMEbtnUpload.FileName);
            var path = Path.Combine(Server.MapPath("~/App_Data/Given Excel's"), fileName);
            NAMEbtnUpload.SaveAs(path);
        }

        return RedirectToAction("Index");
    }

but this shows following error:

Server Error in '/' Application.

The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /

4

4 回答 4

14

尝试将“EncType”属性添加到您的表单中。

@using (Html.BeginForm("ACTION", "CONTROLLER", FormMethod.Post, new { EncType="multipart/form-data"})){
  //FORM MARKUP HERE
}
于 2013-10-03T16:26:57.370 回答
10

Phil Haack 在他的博客文章Uploading a File (Or Files) With ASP.NET MVC中向您展示了如何处理文件上传。

您缺少很多东西,因此阅读该帖子将使您比这里的任何答案更进一步。

** 编辑 1 的更新 **

几个问题

  1. <form action="index" > - 这应该是<form action="/ControllerName/Index">
  2. 您有多个嵌套的表单标签。您可以有多个表单标签,但它们不能嵌套。在您的情况下,您只需要一个。大多数时候你只需要1。
  3. <input type="button" value="Upload" id="IDbtnUpload" name="NAMEbtnUpload"/>应该

@using(Html.BeginForm())与手动编写表单标签相比,使用它更传统。见下文。

@using(Html.BeginForm("Index"))
{
 <table>
    <tr>
        <td>Upload Excel File: </td>
        <td><input type="text" name="NAMEtxtFileName"/></td>
        <td><input type="file" id="IDbtnUpload" name="NAMEbtnUpload"/></td>

    </tr>
    <tr>
        <td>Company Name: </td>
        <td><input type="text" /></td>
        <td></td>
    </tr>
    <tr>
        <td></td>
        <td align="right"><input type="submit" value="Process" /></td>
        <td></td>
    </tr>
 </table>
}
于 2013-01-08T14:35:21.707 回答
4

单击[上传]按钮,我们将在此页面中,将打开一个文件上传对话框并选择文件,文件位置将显示在 NAMEtxtFileName 文本框中。

这是不可能的,因为文件上传元素不再以编程方式访问。“回到过去”,恶意网站通过将文件上传控件的值设置为众所周知的密码文件位置等,默默地上传敏感信息。

<input type="file" />正如@Bretts 答案中的链接所建议的那样,您只需在表单上添加一个并处理上传服务器端。

于 2013-01-08T14:34:53.597 回答
1

在控制器类中设置文件控件的名称。例如在上面的代码中

public ActionResult Index(HttpPostedFileBase NAMEbtnUpload)

将 NAMEbtnUpload 更改为 NAMEtxtFileName 这可以解决您的问题。

于 2013-03-15T09:08:20.527 回答