0

我正在尝试使用以下内容来允许用户将照片拖到页面上并上传这些照片。

http://alex-tech-adventures.com/development/x-html--css--javascript/97-drag-and-drop-upload-using-html5-with-firefox.html

现在我一直在尝试让模型绑定工作,但到目前为止,内置的任何东西都没有太多运气。有谁知道我怎样才能让它工作???

作为备份,我知道我使用 InputStream 将发送的数据作为字符串提取出来,然后 sdserialize 到我的对象中......

var stream = this.Request.InputStream;
var result = "";
using (var reader = new StreamReader(stream))
{
    result = reader.ReadToEnd();
}
var serializer = new JavaScriptSerializer(); 
var typedObjectResult = serializer.Deserialize<UploadInput>(result);

但是我正在将消息的图像部分转换为字节数组,然后将其保存到文件中。图像的字符串内容如下所示。

data:image/jpeg;base64,/9j/4RjhRXhpZg........3Xuve9de6//9k=

如何将此保存为图像?我应该能够将字节数组写入文件吗?

但我主要关心的是让模型绑定正确。

干杯

4

1 回答 1

5

好的,让我们付诸行动。一如既往地从定义视图模型开始:

public class ImageData
{
    public string Description { get; set; }
    public string Filename { get; set; }
    public string Image { get; set; }
}

然后是控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(IEnumerable<ImageData> images)
    {
        foreach (var item in images ?? Enumerable.Empty<ImageData>())
        {
            var tokens = item.Image.Split(',');
            if (tokens.Length > 1)
            {
                var buffer = Convert.FromBase64String(tokens[1]);
                var file = Path.Combine(HttpContext.Server.MapPath("~/"), item.Filename);
                System.IO.File.WriteAllBytes(file, buffer);
            }
        }
        return Json(new { Status = "OK" });
    }
}

最后是一个观点:

<div id="uploadArea" style="background-color: yellow; width: 170px; height: 50px;">
    drop images here
</div>

@Html.ActionLink("Upload images", "index", null, new { id = "upload" })
<div id="imagesContainer" />


<script type="text/javascript">
    $('#uploadArea').bind('dragover', function (event) {
        event.preventDefault();
    }).bind('drop', function (event) {
        event.preventDefault();
        var files = event.originalEvent.dataTransfer.files;
        $.each(files, function (index, file) {
            var img = $('<img/>')
                .addClass('droppedImage')
                .attr('data-filename', file.name);
            $('#imagesContainer').append(img);
            img.file = file;
            var reader = new FileReader();
            reader.onloadend = function () {
                img.attr('src', reader.result);
            }
            reader.readAsDataURL(file);
        });
    });

    $('#upload').click(function () {
        var imagesJson = $('.droppedImage').map(function () {
            var $this = $(this);
            return {
                image: $this.attr('src'),
                filename: $this.attr('data-filename')
            };
        }).toArray();

        $.ajax({
            url: this.href,
            type: 'POST',
            data: JSON.stringify({ images: imagesJson }),
            contentType: 'application/json',
            success: function (result) {
                alert('success');
            }
        });
        return false;
    });
</script>

现在启动您的 HTML 5 兼容浏览器,尽情享受吧。

于 2011-02-13T20:56:16.920 回答