从控制器中的div读取图像

时间:2013-09-22 14:48:09

标签: asp.net-mvc-4 html image-preloader

我创建了一个索引页

<body>
    <div>
        <script src="~/Scripts/jquery-1.7.1.js"></script>
   <style>
  .thumb {
    height: 75px;
    border: 1px solid #000;
    margin: 10px 5px 0 0;
  }
</style>
@using (Html.BeginForm())
{
<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>

     <input type="submit" name="Post" />
}
<script>
    function handleFileSelect(evt) {
        var files = evt.target.files; // FileList object

        // Loop through the FileList and render image files as thumbnails.
        for (var i = 0, f; f = files[i]; i++) {

            // Only process image files.
            if (!f.type.match('image.*')) {
                continue;
            }

            var reader = new FileReader();

            // Closure to capture the file information.
            reader.onload = (function (theFile) {
                return function (e) {
                    // Render thumbnail.
                    var span = document.createElement('span');
                    span.innerHTML = ['<img class="thumb" src="', e.target.result,
                                      '" title="', escape(theFile.name), '"/>'].join('');
                    document.getElementById('list').insertBefore(span, null);
                };
            })(f);

            // Read in the image file as a data URL.
            reader.readAsDataURL(f);
        }
    }

    document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
    </div>
</body>

控制器

  [HttpPost]
        [ActionName("Index")]
        public ActionResult Index_Post(FormCollection collection)
        {
            var tlist = collection["list"];
            return View();
        }

上传图片时,这就是它在chrome中的样子

enter image description here

但是使用视图源我得到的是渲染输出

<form action="/imgUpload" method="post"><input type="file" id="files" name="files[]" multiple />

     

如何读取此div中的所有图像并将其保存到磁盘?

修改

示例脚本:http://www.html5rocks.com/en/tutorials/file/dndfiles/

同样的问题我可以在那里看到

1 个答案:

答案 0 :(得分:0)

首先更改表单助手

@using (Html.BeginForm("Action",
                       "Controller", FormMethod.Post,
                        new { enctype = "multipart/form-data" }))

然后在控制器中,您可以从Request.Files访问所有发布的文件,或者您可以更改控制器的参数

相关问题