如何上传图像MVC3一个表单中使用ADO.NET到SQL? [英] How to upload image to SQL with MVC3 within one Form using ADO.NET?

查看:87
本文介绍了如何上传图像MVC3一个表单中使用ADO.NET到SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将图像上传字段的字段集的其余部分中,主要becaouse我需要两个动作为一个表单,纠正我,如果我错了! Aloso使用模型来呈现在视场的时候,没有@ Html.UploadField或类似....任何想法?

I am having trouble putting image upload field within the rest of the fieldset, mainly becaouse i need two Actions for one Form, correct me if I am wrong! Aloso when using Model to render fields in View, there is no @Html.UploadField or similar.... Any ideas?

我的目标是在同一行中保存图像为形式的其余部分...

My goal is to save image in the same row as the rest of the form...

MODEL:

 public class MyModel
 {
    [HiddenInput(DisplayValue = false)]
    public int ProductID { get; set; }

    [Display(Name = "Author: ")]
    public string Author { get; set; }

    [Display(Name = "Title: ")]
    public string Title { get; set; }

    public byte[] ImageData { get; set; }

    [HiddenInput(DisplayValue = false)]
    public string ImageMimeType { get; set; }

}

查看:

@model namespace.MyModel

@using (Html.BeginForm("AddForm", "Admin"))
{

<div>
    <fieldset>
        <legend>New Article</legend>

        <div class="editor-label">
            @Html.LabelFor(m => m.Author)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.Author)
            @Html.ValidationMessageFor(m => m.Author)
        </div>

        <div class="editor-label">
            @Html.LabelFor(m => m.Title)
        </div> 
         <div class="editor-field">
            @Html.TextBoxFor(m => m.Title)
        </div>

         @using (Html.BeginForm("SaveImage", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {   
            <div class="editor-label">Image</div>
            <div class="editor-field">
                <div>Upload image: <input type="file" name="Image" /></div>
            </div>

        }

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

控制器

 //AddForm
    [HttpPost]
    public ActionResult AddForm(MyModel model)
    {
        if (ModelState.IsValid)
        {

            string connStr = ConfigurationManager.ConnectionStrings["AdminServices"].ConnectionString;

            using (SqlConnection connection = new SqlConnection(connStr))
            {
                connection.Open();

                using (SqlCommand command = new SqlCommand("insert into MyTable(DbAuthor, DbTitle) values(@author, @title)", connection))
                {
                    command.Parameters.AddWithValue("@author", model.Author);
                    command.Parameters.AddWithValue("@title", model.Title);

                    command.ExecuteNonQuery();

                }
            }
        }

        return RedirectToAction("Index", "Admin");
    }

    //SaveImage
    [Authorize]
    [HttpPost]
    public ActionResult SaveImage(MyModel zan, HttpPostedFileBase image)
    {
        if (ModelState.IsValid)
        {
            if (image != null)
            {
                zan.ImageMimeType = image.ContentType;
                zan.ImageData = new byte[image.ContentLength];
                image.InputStream.Read(zan.ImageData, 0, image.ContentLength);


                string connStr = ConfigurationManager.ConnectionStrings["AdminServices"].ConnectionString;

                using (SqlConnection connection = new SqlConnection(connStr))
                {
                    connection.Open();

                    using (SqlCommand command = new SqlCommand("insert into MyTable(DbImage) values(@image)", connection))
                    {
                        command.Parameters.AddWithValue("@image", zan.ImageData);
                        command.ExecuteNonQuery();

                    }
                }
                return RedirectToAction("Index", "Admin");
            }

        }
        return RedirectToAction("Index", "Admin");
    }

我的问题是把这两种方法为一体,并在浏览本机MVC3 uploadfield ...

My problem is to put these two methods into one, and to have native MVC3 uploadfield in View...

推荐答案

在您的视图模型,声明图像领域作为HttpPostedFileBase像这样:

In your viewmodel, declare the image field as an HttpPostedFileBase like so:

public HttpPostedFileBase ImageData { get; set; }

在视图中,请确保您有两个文件输入ID和NAME属性对应到您的视图模型属性的名称:

In the view, make sure you have both the ID and NAME attributes of the file input correspond to the name of the property on your viewmodel:

<input type="file" name="ImageData" id="ImageData" />

当您提交表单,上传的文件将被填充到由默认的模型绑定的HttpPostedFileBase财产。

When you submit the form, the uploaded file will be populated into the HttpPostedFileBase property by the default model binder.

另外,你的Html.Form需要包装所有输入字段,而不仅仅是文件上传。有嵌套的表格是HTML无效。

Also, your Html.Form needs to wrap all of the input fields, not just the file upload. Having nested forms is invalid in HTML.

@using (Html.BeginForm("AddForm", "Admin", FormMethod.Post, 
    new { enctype = "multipart/form-data" }))
{
<div>
    <fieldset>
        <legend>New Article</legend>

        <div class="editor-label">
            @Html.LabelFor(m => m.Author)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.Author)
            @Html.ValidationMessageFor(m => m.Author)
        </div>

        <div class="editor-label">
            @Html.LabelFor(m => m.Title)
        </div> 
         <div class="editor-field">
            @Html.TextBoxFor(m => m.Title)
        </div>  
        <div class="editor-label">Image</div>
        <div class="editor-field">
            <div>Upload image: 
                <input type="file" name="ImageData" id="ImageData" />
            </div>
        </div>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
</div>
}

更新

我不能回答这个问题的ADO.NET的一部分。我们使用EF 4.1与上面的方法,通过实体属性保存到数据库表。 EF负责的ADO.NET部分对我们来说,所以我不知道code会是怎样使用裸ADO.NET。

I cannot answer the ADO.NET part of the question. We use EF 4.1 with the above approach, saving to a database table through an entity property. EF takes care of the ADO.NET part for us, so I'm not sure what the code would be using bare ADO.NET.

这篇关于如何上传图像MVC3一个表单中使用ADO.NET到SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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