asp.net-mvc 在 ASP.NET MVC3 中使用 Razor 的文件上传控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4681727/
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-09-08 00:43:10  来源:igfitidea点击:

File Upload Controls using Razor in ASP.NET MVC3

asp.net-mvcasp.net-mvc-3file-uploadrazor

提问by Jimmy

Is there a way to define the file upload controls using a Razor helper in ASP.NET MVC3?

有没有办法在 ASP.NET MVC3 中使用 Razor 助手来定义文件上传控件?

回答by Jimmy

There isn't an html helper for file inputs, but what is wrong with just doing

没有用于文件输入的 html 帮助程序,但是这样做有什么问题

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new {enctype = "multipart/form-data"})) {
    <input type='file' name='blah' id='blah' />
}

回答by Don

There is a FileUpload class in Microsoft.Web.Helpers... http://msdn.microsoft.com/en-us/library/microsoft.web.helpers.fileupload(v=vs.99).aspx

Microsoft.Web.Helpers 中有一个 FileUpload 类... http://msdn.microsoft.com/en-us/library/microsoft.web.helpers.fileupload(v=vs.99).aspx

The best/only way I've found to get it is by using NuGet in VisualStudio. Search for package "microsoft-web-helpers" in the online repository. There is one problem I encountered, however. One of the package's dependencies is Facebook.Helper, which it will install at the same time. It will place a file called "Facebook???.cshtml" (forgot the exact name) in your project's AppCode directory. The problem is that the Facebook???.cshtml had some WebMatrix dependencies that I didn't have and didn't want to install. Simply deleting the Facebook.cshtml file (which I wasn't going to use, anyway) seemed to resolve the issue. After that, I was able to compile and debug as usual and use the FileUpload class.

我发现获得它的最佳/唯一方法是在 VisualStudio 中使用 NuGet。在在线存储库中搜索包“microsoft-web-helpers”。但是,我遇到了一个问题。该包的依赖项之一是 Facebook.Helper,它将同时安装。它将在您项目的 AppCode 目录中放置一个名为“Facebook???.cshtml”(忘记确切名称)的文件。问题是 Facebook???.cshtml 有一些我没有也不想安装的 WebMatrix 依赖项。简单地删除 Facebook.cshtml 文件(无论如何我不会使用它)似乎可以解决问题。之后,我可以像往常一样编译和调试并使用 FileUpload 类。

Here's a tutorial I found that utilizes it:

这是我发现的一个使用它的教程:

http://blog.tallan.com/2011/02/04/using-mvc3-razor-helpers-and-jcrop-to-upload-and-crop-images/

http://blog.tallan.com/2011/02/04/using-mvc3-razor-helpers-and-jcrop-to-upload-and-crop-images/

回答by jarimos

USING RAZOR

使用剃刀

@*requieres installing Asp helpers / you can do it her from NuGet or logging som admin in packages*@
@using Microsoft.Web.Helpers;
@{
    var fileName = "";
    if (IsPost) {
        var fileSavePath = "";
        var uploadedFile = Request.Files[0];
        fileName = Path.GetFileName(uploadedFile.FileName);
        fileSavePath = Server.MapPath("~/UploadedFiles/" +
          fileName);
        uploadedFile.SaveAs(fileSavePath);
    }
}

      @FileUpload.GetHtml(
        initialNumberOfFiles:1,
        allowMoreFilesToBeAdded:false, 
        includeFormTag:false,
        name: "Upload1",
        uploadText:"Upload")

    @if (IsPost) {
        <span>File uploaded!</span><br/>
    }