Summernote上传文件到服务器

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为空。
有些例子说这样可以工作,但我的代码在功能上不起作用!
有什么想法?

你修好了吗? - Stefanvds
你的console.log(file)的结果是什么?我认为这个值无法正确地转换为HttpPostedFileBase。 - Michael Samteladze
1个回答

0
在你的方法中使用 "Request" 对象来获取文件,就像这样:
        [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;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接