asp.net-mvc - MVC-- 文件上传

标签 asp.net-mvc file-upload

嘿... 我对我的 View 有上传控制。有没有办法将此控件与模型数据关联(例如 LabelFor 或 TextBoxFor)。我需要这个,因为在页面加载时我丢失了文件上传控件中的信息 谢谢

最佳答案

HTML 上传文件 ASP MVC 3。

模型:(请注意,FileExtensionsAttribute 在 MvcFutures 中可用。它将验证文件扩展名客户端和服务器端。)

public class ViewModel
{
    [Required, Microsoft.Web.Mvc.FileExtensions(Extensions = "csv", ErrorMessage = "Specify a CSV file. (Comma-separated values)")]
    public HttpPostedFileBase File { get; set; }
}

HTML View :

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.TextBoxFor(m => m.File, new { type = "file" })
    @Html.ValidationMessageFor(m => m.File)
}

Controller 操作:

[HttpPost]
public ActionResult Action(ViewModel model)
{
    if (ModelState.IsValid)
    {
        // Use your file here
        using (MemoryStream memoryStream = new MemoryStream())
        {
            model.File.InputStream.CopyTo(memoryStream);
        }
    }
}

关于asp.net-mvc - MVC-- 文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4658081/

相关文章:

asp.net-mvc - CaSTLe Windsor 无法在中等信任下运行

php - 如何在 PHP 中使用来自两个不同服务器的 MySQL 数据库?

javascript - 选择单选按钮时必须输入文本框

javascript - 具有 MVC 模型的完整日历事件数组

c# - Web API + jQuery AJAX DELETE 请求返回 404

c# - 如何从 Controller 重定向到外部 url?

在 php.ini 上更改 upload_max_filesize 后,PHP 文件上传大小受到限制

html - 如何获取 html5 文件输入以跨浏览器一致地仅接受某些文件类型?

java - Struts 2中通过表上传文件

file-upload - 使用 Content-Type multipart/form-data POST 数据