5

我目前正在按照说明处理从 ASP.NET MVC Razor 中的 Summernote 控件上传的图像。

服务器代码:

[HttpPost]
    public ActionResult UploadImage(HttpPostedFileBase file)
    {
        var imageUrl = UpdateProfilePicture(file);
        return Json(imageUrl);
    }

客户端ajax:

<script>
$('.summernote').summernote({
    height: 200,
    focus: true, onImageUpload: function (files, editor, welEditable) {
        sendFile(files[0], editor, welEditable);
    }
});
function sendFile(file, editor, welEditable) {

    console.log(file);
    $.ajax({
        data: {file:file},
        type: "POST",
        url: "@Url.Action("UploadImage","Message")",
        cache: false,
        contentType: false,
        processData: false,
        success: function (url) {
            editor.insertImage(welEditable, url);
        }
    });
}

我在服务器端方法得到结果,但参数HttpPostedFileBase file为空

一些例子说这有效,但我的代码在功能上不起作用!

有任何想法吗 ?

4

1 回答 1

0

在您的方法中使用“请求”对象来获取这样的文件:

        [HttpPost]
    public string UploadImage()
    {
        for (int i = 0; i < Request.Files.Count; i++)
        {
            var file = Request.Files[i];

            var fileName = Path.GetFileName(file.FileName);

            var path = Path.Combine(Server.MapPath("~/Images/"), fileName);
            file.SaveAs(path);
        }
        return YOUR UPLOADED IMAGE URL;
    }
于 2016-03-02T07:45:48.683 回答