C# 在asp.net mvc 4 razor中上传文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14217240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:05:39  来源:igfitidea点击:

file upload in asp.net mvc 4 razor

c#file-uploadrazorasp.net-mvc-4

提问by Abdur Rahim

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

我正在使用ASP .Net MVC 4.0VS10。我是网络应用程序的新手。

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

我设计了一个带有 html razor 视图的页面。这是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 NAMEtxtFileNametextbox.

我正在尝试在NAMEbtnUpload的点击事件中上传一个 excel 文件。单击此按钮我们将进入此页面,将打开一个文件上传对话框并选择文件,文件位置将显示在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. 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.

无法找到该资源。描述HTTP 404。您正在寻找的资源(或其依赖项之一)可能已被删除、更改名称或暂时不可用。请检查以下 URL 并确保其拼写正确。

Requested URL:/

请求的网址:/

回答by CodeCaster

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 回答的链接中所建议的那样。

回答by Brett Allred

Phil Haack shows you how to handle file uploads with his blog post Uploading a File (Or Files) With ASP.NET MVC.

Phil Haack 通过他的博客文章Uploading a File (Or Files) With 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">
  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.
  3. <input type="button" value="Upload" id="IDbtnUpload" name="NAMEbtnUpload"/>should be
  1. <form action="index" >- 这应该是 <form action="/ControllerName/Index">
  2. 您有多个嵌套的表单标签。您可以有多个表单标签,但它们不能嵌套。在您的情况下,您只需要一个。大多数情况下,您只需要 1 个。
  3. <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. See below.

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

回答by Esha Garg

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 这可以解决您的问题。

回答by Hemslingo

Try adding the "EncType" attribute to your form.

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

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