asp.net - 文件上传到 MySQL DB + ASP.NET MVC4

标签 asp.net mysql asp.net-mvc asp.net-mvc-4 file-upload

我有一个关于使用 ASP.NET MVC 将文件上传到 SQL 数据库的问题。
这是我要存储图像的表:

Table "deliverables"
- item_id
- deliverable_image

item_id 来自另一个表,我在其中存储图像的名称、描述、标签等!

这是我的 DeliverableController 创建 View :

@model GDMfrontEnd.Models.DeliverableViewModel

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>EventViewModel</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Thumbnail)
        </div>
        <div class="editor-field">
            <!--
            @Html.EditorFor(model => model.Thumbnail)
            @Html.ValidationMessageFor(model => model.Thumbnail)
            -->
            <form action="/profile/upload"  method="post" enctype="multipart/form-data">
              <label for="photo">Photo:</label>
              <input type="file" name="photo" id="photo" />

              <input type="submit" value="Upload" />
            </form>
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Image)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Image)
            @Html.ValidationMessageFor(model => model.Image)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.VideoUrl)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.VideoUrl)
            @Html.ValidationMessageFor(model => model.VideoUrl)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

    @Scripts.Render("~/bundles/jqueryui")
    @Styles.Render("~/Content/themes/base/css")
    </script>
}

我的 DeliverableViewModel 看起来像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace GDMfrontEnd.Models
{
    public class DeliverableViewModel
    {
        [Required]
        [Display(Name = "Title")]
        public string Title { get; set; }

        [Required]
        [Display(Name = "Description")]
        public string Description { get; set; }

        [Required]
        [Display(Name = "Thumbnail")]
        public byte[] Thumbnail { get; set; }

        [Required]
        [Display(Name = "Image")]
        public byte[] Image { get; set; }

        [Required]
        [Display(Name = "VideoUrl")]
        public string VideoUrl { get; set; }

        public long UsernameID { get; set; }
    }
}

这是我的连接字符串:

  <connectionStrings>
    <add name="gdmwebsiteEntities" connectionString="metadata=res://*/Models.DBModel.csdl|res://*/Models.DBModel.ssdl|res://*/Models.DBModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;User Id=root;database=gdmwebsite&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

但是如何上传文件而不仅仅是文本?
我已经完成了一些教程,但没有一个非常清晰,也没有一个可以与 MySQL 数据库一起使用。

尼尔斯

最佳答案

为了完成你想要的,不管我不喜欢在数据库中保存字节数组

首先更改在表单中添加以下参数,这样您就可以发送图像

@using (Html.BeginForm(null,null ,new { @enctype= "multipart/form-data"}))

修改了模型中映射的两个属性,从字节数组更改为类型 HttpPostedFileBase

public HttpPostedFileBase Image { get; set; }
public HttpPostedFileBase Thumbnail  { get; set; }

在 View 中添加属性,例如

@Html.TextboxFor(model => model.Thumbnail, new {type="file"})
@Html.TextboxFor(model => model.Image , new {type="file"})

删除不应有两个嵌套表单的内部表单,

现在,当您收到值时,您可以使用 SaveAs 方法保存或转换为字节数组

model.Image.SaveAs();

了解更多信息here's the doc

关于asp.net - 文件上传到 MySQL DB + ASP.NET MVC4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16801357/

相关文章:

c# - 服务器控件、客户端控件、面板、默认按钮和客户端事件

asp.net - 实现 URL 路由时防止对 .ASPX 页面的正常请求

php - 如何在 PHPExcel 中选择特定的列顺序

c# - 我可以在 ASP.NET Core 中使用 Content Negotiation 向浏览器返回 View 并向 API 调用返回 JSON 吗?

asp.net - 将外键应用于表时mysql中的错误代码1005

Java Servlet 静态类

mysql - 无法使用 .NET mysql 连接器连接到 mysql rds 实例

php - 将数据库中的信息排序到表中

jquery - MVC 和 jQuery 验证, 'weave' javascript 在哪里以及如何嵌入母版页?

asp.net-mvc - ASP.NET MVC 2 IgnoreRoute 在所有目录中