简体   繁体   English

在asp.net mvc 4 razor上传文件

[英]file upload in asp.net mvc 4 razor

I am using ASP .Net MVC 4.0 and VS10 . 我正在使用ASP .Net MVC 4.0VS10 I am a newbie in web application. 我是网络应用程序的新手。

I have designed a page with html razor view. 我设计了一个带有hz剃刀视图的页面。 Here is some code of Index.cshtml : 以下是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. 我想在NAMEbtnUpload的点击事件中上传一个excel文件。 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. 单击此按钮我们将在此页面中,只打开文件上传对话框并选择文件,文件位置将显示在NAMEtxtFileName文本框中。

EDIT 1: 编辑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 . 描述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: / 请求的网址: /

Try adding the "EncType" attribute to your form. 尝试将“EncType”属性添加到表单中。

@using (Html.BeginForm("ACTION", "CONTROLLER", FormMethod.Post, new { EncType="multipart/form-data"})){
  //FORM MARKUP HERE
}

Phil Haack shows you how to handle file uploads with his blog post Uploading a File (Or Files) With ASP.NET MVC . Phil Haack通过他的博客文章使用ASP.NET MVC上传文件(或文件)向您展示如何处理文件上传。

There is quite a bit of stuff you are missing so reading that post will get you further than any answer here. 你有很多东西都没有,所以阅读这篇文章会让你比任何答案更进一步。

** UPDATE FOR EDIT 1 ** **编辑更新1 **

A couple issues 几个问题

  1. <form action="index" > - this should be <form action="/ControllerName/Index"> <form action="index" > - 这应该是<form action="/ControllerName/Index">
  2. You have multiple form tags that are nested. 您有多个嵌套的表单标记。 You can have multiple form tags but they can't be nested. 您可以拥有多个表单标记,但它们不能嵌套。 In your case your only need one. 在你的情况下,你唯一需要一个。 Most of the time you only need 1. 大多数时候你只需要1。
  3. <input type="button" value="Upload" id="IDbtnUpload" name="NAMEbtnUpload"/> should be <input type="button" value="Upload" id="IDbtnUpload" name="NAMEbtnUpload"/>应该是

It is more conventional to use @using(Html.BeginForm()) as opposed to manually writing form tags. 与手动编写表单标签相比,使用@using(Html.BeginForm())更常规。 See below. 见下文。

@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>
}

clicking on [the upload] 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. 单击[上传]按钮,我们将在此页面中,只需打开文件上传对话框并选择文件,文件位置将显示在NAMEtxtFileName文本框中。

That is not possible because a file upload element is not accessible programatically, anymore. 这是不可能的,因为文件上载元素不再以编程方式访问。 "Back in the days" it was, and malicious sites silently uploaded sensitive information by setting the file upload control's value to well known password file locations and so on. “回到过去”,恶意网站通过将文件上传控件的值设置为众所周知的密码文件位置等来静默上传敏感信息。

You'll just have to put an <input type="file" /> on your form and handle the upload serverside, as suggested in the link on @Bretts answer. 您只需在表单上放置<input type="file" />并处理上传服务器端,如@Bretts答案中的链接所示。

Set the name of file control in controller class. 在控制器类中设置文件控件的名称。 for example in above code 例如在上面的代码中

public ActionResult Index(HttpPostedFileBase NAMEbtnUpload)

change NAMEbtnUpload to NAMEtxtFileName this resolve your problem. 将NAMEbtnUpload更改为NAMEtxtFileName这可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM