我目前正在按照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
为空
一些例子说可行,但我的代码在功能上不起作用!
有什么想法吗?
答案 0 :(得分:0)
在您的方法中使用&#34;请求&#34;对象获取这样的文件:
[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;
}