Summernote上传文件到服务器
问题描述:
我目前做了以下指令来处理从ASP.NET MVC Razor中的summernote控件上传图片。Summernote上传文件到服务器
Server代码:
[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
为null
一些例子说,这有效,但我的代码功能不工作!
任何想法?
答
在你的方法使用“请求”对象来获得这样的文件:
[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;
}
你设法解决这个问题? – Stefanvds 2015-05-14 06:08:47
你的console.log(文件)的成就是什么?我认为该值无法正确转换为HttpPostedFileBase – 2016-02-21 09:15:39