2016-01-21 69 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

相关问题