在mvc中将图像存储在数据库中

时间:2013-10-09 15:39:36

标签: asp.net-mvc file-upload

我正在尝试构建一个电子商务应用程序。我的任务是存储用户上传的图像

从他的机器进入oracle数据库。我为列

创建了一个blob数据类型

存储图像。现在

查看代码

@using (Html.BeginForm())
{
  <div class="editor-field">
                @Html.TextBoxFor(m => m.Description, new { id = "txtDescription" })
                @Html.ValidationMessageFor(m => m.Description)
            </div>
           <div class="editor-label">
                @Html.Label("Upload your picture");
            </div>
            <p>
                <input type="file"  id="imagesubmit" name="imagesubmit" value="Submit"  
onclick="saveuserinfo();"/>
            </p>
}

javascript代码

function saveuserinfo() 
{
        var description = document.getElementById('txtDescription').value;
        var imagefile = document.getElementById('imagesubmit');
        $.ajax({
            url: '@Url.Action("SaveuserInfo", "Welcome")',
            data: { description: description,imagesubmit: imagesubmit},
            type: 'POST',
            success: function (data) {
                            }
        });
    }

控制器代码

???

我需要控制器代码来了解如何从javascript获取图像并将其传递给DB层以便插入

进入数据库。

这就是我一直在尝试的..这是存储图像的正确方法

public ActionResult saveuserinfo(string description,)
    {
        return View("WelcomePage");
    }

(我在这里遇到的代码是什么数据类型我必须在这里用来接收图像和什么

数据类型我必须转换并将其发送到DB LAYER。

2 个答案:

答案 0 :(得分:0)

您需要在操作中添加MultifilePart参数:

 public ActionResult saveuserinfo(@RequestParam MultipartFile image) {...}

该参数具有允许您访问字节的方法。注意:您只能阅读一次流!

文档:http://docs.spring.io/spring/docs/3.2.1.RELEASE/spring-framework-reference/html/mvc.html#mvc-multipart

答案 1 :(得分:0)

您无法使用jquery ajax或任何形式的纯XMLhttprequest上传文件。

你应该使用

@using (Html.BeginForm("YourAction", "YourController", FormMethod.Post, new { enctype = multipart/form-data" })) {
// your form elements goes here, e.g <input type="file" .....
}

或者,如果你真的想要ajax感觉上传,你应该看看Fine Uploader:https://github.com/Widen/fine-uploader