ASP.NET MVC 4 C# HttpPostedFileBase,我如何存储文件 [英] ASP.NET MVC 4 C# HttpPostedFileBase, How do I Store File

查看:24
本文介绍了ASP.NET MVC 4 C# HttpPostedFileBase,我如何存储文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模型

公共部分类赋值{公共分配(){this.CourseAvailables = new HashSet();}公共字符串分配 ID { 获取;放;}public Nullable提交日期 { 得到;放;}公共字符串状态 { 获取;放;}公共可空<十进制>马克{得到;放;}公共字符串注释 { 获取;放;}公共字符串 FileLocation { 获取;放;}公共虚拟 ICollectionCourseAvailables { 得到;放;}}}

控制器

 public ActionResult Create(Assignment assignment){如果(模型状态.IsValid){db.Assignments.Add(assignment);db.SaveChanges();return RedirectToAction("索引");}返回视图(分配);}

查看

<%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%><%: Html.ValidationMessageFor(model =>model.FileLocation)%>

如果我想将文件存储到服务器/路径文件夹中,而在数据库中我只想存储路径名/字符串,我该如何存储文件.

解决方案

您可以像这样上传文件并将其 url 保存在数据库表中:

查看:

@using(Html.BeginForm("Create","Assignment",FormMethod.Post,new {enctype="multipart/form-data"})){...<div class="editor-field"><%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%><%: Html.ValidationMessageFor(model =>model.FileLocation)%>

...}

行动:

[HttpPost]公共 ActionResult 创建(分配分配){如果(模型状态.IsValid){if(Request.Files.Count > 0){HttpPostedFileBase 文件 = Request.Files[0];if (file.ContentLength > 0){var fileName = Path.GetFileName(file.FileName);assignment.FileLocation = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);file.SaveAs(assignment.FileLocation);}db.Assignments.Add(assignment);db.SaveChanges();return RedirectToAction("索引");}}返回视图(分配);}

详情:

为了更好地理解,请参阅这个好文章使用 ASP.NET MVC 上传文件(或文件)

Model

public partial class Assignment
{
    public Assignment()
    {
        this.CourseAvailables = new HashSet<CourseAvailable>();
    }

    public string AssignmentID { get; set; }
    public Nullable<System.DateTime> SubmissionDate { get; set; }
    public string Status { get; set; }
    public Nullable<decimal> Mark { get; set; }
    public string Comments { get; set; }
    public string FileLocation  { get; set; }
    public virtual ICollection<CourseAvailable> CourseAvailables { get; set; }
}}

Controller

 public ActionResult Create(Assignment assignment)
    {
        if (ModelState.IsValid)
        {


            db.Assignments.Add(assignment);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(assignment);
    }

View

<div class="editor-field">
    <%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%>
    <%: Html.ValidationMessageFor(model => model.FileLocation) %>
</div>

How do I store a file if I wanted to store the file into the server/path folder and in the database I only want to store the Path name/string.

解决方案

you can upload file and save its url in the database table like this:

View:

@using(Html.BeginForm("Create","Assignment",FormMethod.Post,new {enctype="multipart/form-data"}))
{
    ...
    <div class="editor-field">
        <%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%>
        <%: Html.ValidationMessageFor(model => model.FileLocation) %>
    </div>
    ...
}

Action:

[HttpPost]
public ActionResult Create(Assignment assignment)
{
    if (ModelState.IsValid)
    {
        if(Request.Files.Count > 0)
        {
            HttpPostedFileBase file = Request.Files[0];
            if (file.ContentLength > 0) 
            {
                var fileName = Path.GetFileName(file.FileName);
                assignment.FileLocation = Path.Combine(
                    Server.MapPath("~/App_Data/uploads"), fileName);
                file.SaveAs(assignment.FileLocation);
            }
            db.Assignments.Add(assignment);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }

    return View(assignment);
}

Details:

For better understanding refer this good article Uploading a File (Or Files) With ASP.NET MVC

这篇关于ASP.NET MVC 4 C# HttpPostedFileBase,我如何存储文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆