2016-01-21 64 views
1

我想使用CKEditor的內置上傳文件,它與我的MVC5項目,但它不適用於我的MVC6項目,上傳代碼該文件是正確的,我已經測試過了,它實際上傳文件到服務器,但它沒有填充URL和圖像信息的形式,下面是我的MVC5項目的代碼:CKEditor文件上傳無法正常工作與mvc 6

public ActionResult UploadImage(HttpPostedFileBase upload, string CKEditorFuncNum, string CKEditor, 
     string langCode) 
    { 
     string vImagePath = String.Empty; 
     string vMessage = String.Empty; 
     string vFilePath = String.Empty; 
     string vOutput = String.Empty; 
     try 
     { 
      if (upload != null && upload.ContentLength > 0) 
      { 
       var vFileName = DateTime.Now.ToString("yyyyMMdd-HHMMssff") + " - " + Path.GetFileName(upload.FileName); 
       var vFolderPath = Server.MapPath("/Upload/"); 
       if (!Directory.Exists(vFolderPath)) 
       { 
        Directory.CreateDirectory(vFolderPath); 
       } 
       vFilePath = Path.Combine(vFolderPath, vFileName); 
       upload.SaveAs(vFilePath); 
       vImagePath = Url.Content("/Upload/" + vFileName); 
       vMessage = "The file uploaded successfully."; 
      } 
     } 
     catch(Exception e) 
     { 
      vMessage = "There was an issue uploading:" + e.Message; 
     } 
     vOutput = @"<html><body><script>window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", \"" + vImagePath + "\", \"" + vMessage + "\");</script></body></html>"; 
     return Content(vOutput); 
    } 

這裏是MVC6項目的代碼不起作用:

public async Task<ActionResult> UploadImage(IFormFile upload, string CKEditorFuncNum, string CKEditor, 
     string langCode) 
    { 
     string vImagePath = String.Empty; 
     string vMessage = String.Empty; 
     string vFilePath = String.Empty; 
     string vOutput = String.Empty; 

     try 
     { 
      if (upload != null && upload.Length > 0) 
      { 
       var vFileName = DateTime.Now.ToString("yyyyMMdd-HHMMssff") + " - " + ContentDispositionHeaderValue.Parse(upload.ContentDisposition).FileName.Trim('"'); 
       var vFolderPath = Path.Combine(_environment.WebRootPath, "Files", "ArticleUploads"); 

       if (!Directory.Exists(vFolderPath)) 
       { 
        Directory.CreateDirectory(vFolderPath); 
       } 

       vFilePath = Path.Combine(vFolderPath, vFileName); 
       await upload.SaveAsAsync(vFilePath); 
       vImagePath = Url.Content("/Files/ArticleUploads/" + vFileName); 
       vMessage = "The file uploaded successfully."; 
      } 
     } 
     catch (Exception e) 
     { 
      vMessage = "There was an issue uploading:" + e.Message; 
     } 
     vOutput = @"<html><body><script>window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", \"" + vImagePath + "\", \"" + vMessage + "\");</script></body></html>"; 
     return Content(vOutput); 
    } 

而且在CKEditor的配置文件我有:

config.filebrowserImageUploadUrl = '/Admin/Article/UploadImage'; 

我已經檢查了變數,並且他們發送相同的價值,也值得大家注意的是我使用CKEditor的相同版本,所以不能成爲問題,我會很感激這方面的幫助。

+0

我想這樣做,但它不起作用。你可以給我發送示例代碼嗎? @Deckard –

回答

3

如果文件上傳並且您沒有看到圖像被填充,我想你的內容返回方式應該有問題,因爲你正在返回html,請嘗試指定你的內容類型,就像這樣:

return Content(vOutput, "text/html"); 

如果沒有解決您的問題,您需要提供更多的信息,告訴我們究竟你在JavaScript端這一行動得到。

+0

謝謝!有效。 – Deckard

相關問題