2015-02-09 203 views
5

我目前做了以下指令來處理從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

一些例子說,這有效,但我的代碼功能不工作!

任何想法?

+0

你設法解決這個問題? – Stefanvds 2015-05-14 06:08:47

+0

你的console.log(文件)的成就是什麼?我認爲該值無法正確轉換爲HttpPostedFileBase – 2016-02-21 09:15:39

回答

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; 
    }