首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CKEditor文件上传在mvc 6中无法正常工作

CKEditor文件上传在mvc 6中无法正常工作
EN

Stack Overflow用户
提问于 2016-01-21 08:02:25
回答 1查看 1.5K关注 0票数 1

我尝试使用内置的CKEditor上传文件,它适用于我的MVC5项目,但它不适用于我的MVC6项目,上传文件的代码是正确的,我已经测试过它,它实际上将文件上传到服务器,但是它没有用URL和图像信息填充表单,下面是我的MVC5项目的代码:

代码语言:javascript
复制
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项目的代码:

代码语言:javascript
复制
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配置文件中,我有:

代码语言:javascript
复制
config.filebrowserImageUploadUrl = '/Admin/Article/UploadImage';

我检查了变量,它们发送了相同的值,同样值得注意的是,我使用的是相同版本的CKEditor,所以这不会是问题所在,我希望能在这方面提供任何帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-21 16:33:55

如果文件被上传,而您没有看到图像被填充,我想您返回内容的方式会出现一些问题,因为您正在返回html,请尝试指定内容类型,如下所示:

代码语言:javascript
复制
return Content(vOutput, "text/html");

如果这不能解决您的问题,您需要提供更多的信息,告诉我们您从JavaScript端的这个操作中到底得到了什么。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34918296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档