SAE上是没有文件夹读写权限的,所以要在SAE使用Ueditor的图片上传功能须要借助SAE的Storage服务。

  一、开通Storage服务

  在SAE控制台开通Storage服务,并新增一个domain。

二、改动Ueditor代码

  Ueditor处理上传文件的方法在DjangoUeditor/jviews.py中。上传图片的请求是由以下函数处理的

#上传附件
@csrf_exempt
def UploadFile(request,uploadtype,uploadpath):
    '''
    省略若干代码
    '''
    #检測保存路径是否存在,假设不存在则须要创建
    OutputPath=os.path.join(USettings.gSettings.MEDIA_ROOT,os.path.dirname(uploadpath)).replace("//","/")
    if not os.path.exists(OutputPath):
        os.makedirs(OutputPath)
        #要保存的文件名称格式使用"原文件名称_当前时间.扩展名"
    OutputFile=GenerateRndFilename(file.name)
    #全部检測完毕后写入文件
    if state=="SUCCESS":
        #保存到文件里
        state=SaveUploadFile(file,os.path.join(OutputPath,OutputFile))
    #返回数据
    if uploadtype=="image" or uploadtype=="scrawlbg":
        rInfo={
            'url'      :OutputFile,    #保存后的文件名称称
            'title'    :request.POST.get("pictitle",file.name), 
            'original' :file.name,      #原始文件名称
            'state'    :state          
        }
    else:
        rInfo={
            'url'      :OutputFile,         #保存后的文件名称称
            'original' :file.name,         #原始文件名称
            'filetype' :original_ext,
            'state'    :state              
        }
    '''
    省略若干代码
    '''

在进行一系列检查后调用SaveUploadFile方法将文件写入硬盘。

#保存上传的文件
def SaveUploadFile(PostFile,FilePath):
    try:
        f = open(FilePath, 'wb')
        for chunk in PostFile.chunks():
            f.write(chunk)
    except MyException,E:
        f.close()
        return u"写入文件错误:"+ E.message
    f.close()
    return u"SUCCESS"

最后其会将图片的本地路径当做url返回给浏览器,然后浏览器就会把url插入编辑器中。

因此我们须要改动例如以下三处代码:

1、SaveUploadFile方法

def SaveUploadFile(PostFile, path):
    try:
        import sae.const
        access_key = sae.const.ACCESS_KEY
        secret_key = sae.const.SECRET_KEY
        appname = sae.const.APP_NAME
        domain_name = "你的domain"      
        
        import sae.storage
        s = sae.storage.Client()
        ob = sae.storage.Object(PostFile)
        #此处返回的url是文件在Storage上的url
        url = s.put(domain_name, path, ob)
        return u'SUCCESS', url
    except Exception,e:
        return u'上传文件到sae失败',''

2、UploadFile方法

由于文件上传到SAE Storage后url会跟本地的情况不一样。所以我们还要用SaveUploadFile返回的url替换原来当做url返回给浏览器的OutputFile。除此之外还要将校验本地目录是否存在的代码移除(否则在SAE上会产生异常)。

def UploadFile(request,uploadtype,uploadpath):
    '''
    省略...
    '''
    #检測保存路径是否存在,假设不存在则须要创建
    OutputPath=os.path.join(USettings.gSettings.MEDIA_ROOT,
        os.path.dirname(uploadpath)).replace("//","/")
    #将以下两行凝视掉
    #if not os.path.exists(OutputPath):
    #    os.makedirs(OutputPath)
        #要保存的文件名称格式使用"原文件名称_当前时间.扩展名"
    OutputFile=GenerateRndFilename(file.name)
    #全部检測完毕后写入文件
    if state=="SUCCESS":
        #保存到文件里
        '注意此处'
        state, url=SaveUploadFile(file,os.path.join(OutputPath,OutputFile))
    #返回数据     if uploadtype=="image" or uploadtype=="scrawlbg":
        rInfo={
           #注意此处
            'url'      :url,    #保存后的文件名称称
            'title'    :request.POST.get("pictitle",file.name), 
            'original' :file.name,      #原始文件名称
            'state'    :state           
        }
    else:
        rInfo={
           #注意此处
            'url'      :url,         #保存后的文件名称称
            'original' :file.name,         #原始文件名称
            'filetype' :original_ext,
            'state'    :state               
        }
        
    '''
    省略...
    '''

3、html模板

然后还须要将ueditor.html改动成以下的样子。否则Ueditor会在server返回的URL前面加上你的'MEDIA_ROOT'

<textarea name={{ UEditor.name }} id=id_{{ UEditor.name }} 
style="display:inline-block;width:{{ UEditor.width }}px;
{{ UEditor.css }}">{{UEditor.value}}</textarea>
<script type="text/javascript">
     var id_{{ UEditor.name  }}= new baidu.editor.ui.Editor({
         "UEDITOR_HOME_URL":"{{ STATIC_URL }}ueditor/",
         {% ifnotequal UEditor.toolbars None %}"toolbars":
         {{ UEditor.toolbars|safe }},{% endifnotequal %}
         "imageUrl":"/ueditor/ImageUp/{{ UEditor.imagePath }}",
         "imagePath":"",
         "scrawlUrl":"/ueditor/scrawlUp/{{ UEditor.scrawlPath }}",
         "scrawlPath":"",
         "imageManagerUrl":"/ueditor/ImageManager/{{ UEditor.imageManagerPath }}",
         "imageManagerPath":"{{ MEDIA_URL }}{{ UEditor.imageManagerPath }}",
         "catcherUrl":"/ueditor/RemoteCatchImage/{{ UEditor.imagePath }}",
         "catcherPath":"",
         "fileUrl":"/ueditor/FileUp/{{ UEditor.filePath }}",
         "filePath":"",
         "getMovieUrl":"/ueditor/SearchMovie/"
         {% ifnotequal UEditor.options '' %},{{ UEditor.options|safe }}{% endifnotequal %}
     });
     id_{{UEditor.name}}.render('id_{{ UEditor.name }}');
     id_{{UEditor.name}}.addListener('ready',function(){
         id_{{UEditor.name}}.setHeight({{ UEditor.height }});
     });
</script>

这样你就能够在SAE上通过Ueditor将图片上传到SAE Storage上去了。

转载需注明本文地址:http://mushapi.sinaapp.com/use-ueditor-on-sae.html

在SAE上使用Ueditor的图片上传功能的更多相关文章

  1. uEditor独立图片上传

    项目中.上传图片,非常希望有一款比较兼容的查件. 网上找了一些,图片上传立刻显示的js代码,还有uploadify.都会碰到这样那样的不兼容和其它头疼的问题. 后来想,干脆就用php的上传类最干脆.但 ...

  2. springboot整合ueditor实现图片上传和文件上传功能

    springboot整合ueditor实现图片上传和文件上传功能 写在前面: 在阅读本篇之前,请先按照我的这篇随笔完成对ueditor的前期配置工作: springboot+layui 整合百度富文本 ...

  3. 有关于weiphp2.00611上传sae的一些注意(图片上传解决方案)

    一.安装中注意的事项  安装时使用的系统为weiphp2.0611    版本     1.将所有文件上传到代码库中     2.按照步骤进行安装weiphp,注意在数据库导入的时候需要手动导入.  ...

  4. 对百度的UEditor多图片上传的一些补充

    我已经写了一篇文章关于百度的UEditor提取多图片上传模块.如果还没有看过,请点击以下链接查看 http://www.cnblogs.com/luke1006/p/3719029.html 出差了两 ...

  5. 部署新浪SAE web.py Session及图片上传等问题注意事项

    1.以下几条代码解决编码问题 import sysreload(sys)sys.setdefaultencoding('utf-8') 2.图片上传问题 需要开通sina的Storage服务,随便建个 ...

  6. 单独调用Ueditor的图片上传功能

    <!DOCTYPE html> <html> <head> <title></title> <script src="/sc ...

  7. asp.net 百度编辑器 UEditor 上传图片 图片上传配置 编辑器配置 网络连接错误,请检查配置后重试

    1.配置ueditor/editor_config.js文件,将 //图片上传配置区 ,imageUrl:URL+"net/imageUp.ashx" //图片上传提交地址 ,im ...

  8. UEditor使用------图片上传与springMVC集成 完整实例

    UEditor是一个很强大的在线编辑软件 ,首先讲一下 基本的配置使用 ,如果已经会的同学可以直接跳过此节 ,今天篇文章重点说图片上传; 一  富文本的初始化使用: 1 首先将UEditor从官网下载 ...

  9. 百度ueditor的图片上传,前后端交互使用

    百度ueditor的使用 一个文本编辑器,看了网上很多文档写的很乱,这里拾人牙慧,整理下怎么使用. 这个东西如果不涉及到图片附件上传,其实很简单,就是几个前端文件,直接引用,然后配置下ueditor. ...

随机推荐

  1. 【POJ 3090】 Visible Lattice Points

    [题目链接] http://poj.org/problem?id=3090 [算法] 通过观察发现,在这个平面直角坐标系中,除了(1,1),(1,0)和(0,1),所有可见点的横纵坐标互质 那么,问题 ...

  2. Maven打包编译错误工作区间设置编码格式gbk可以utf-8不可以

    转自:https://blog.csdn.net/wolf_love666/article/details/52593483 问题:Maven打包编译错误工作区间设置编码格式gbk可以utf-8不可以 ...

  3. 自定义数据类型 C++ 结构体类型 共同体类型 枚举类型 类类型{}

    一.结构体类型 结构体类型,共用体类型,枚举类型,类类型等统称为自定义类型(user-defined-type,UDT). 结构体相当于其他高级语言中的记录(record);例如: struct St ...

  4. Java实现一个简单的网络爬虫

    Java实现一个简单的网络爬虫 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWri ...

  5. python-sqlite3事务

    sqlite3事务总结: 在connect()中不传入 isolation_level 事务处理: 使用connection.commit() #!/usr/bin/env python # -*- ...

  6. ueditor和flexpaper的学习。。。。

    博客园的博主编辑文本的时候在博客园编辑器上编辑的,最近见到并学习了一点百度一款ueditor的编辑器可供程序猿们二次开发.... 见链接http://fex.baidu.com/ueditor/#st ...

  7. vue-router 原理解析

    “更新视图但不重新请求页面”是前端路由原理的核心之一,

  8. Walking on the path of Redis --- Introduction and Installation

    废话开篇 以前从来没听说过有Redis这么个玩意,无意间看到一位仁兄的博客,才对其有所了解,所以决定对其深入了解下.有不对的地方还请各位指正. Redis介绍 下面是官方的介绍,不喜欢english的 ...

  9. 6 Python+Selenium的元素定位方法(CSS)

    [环境] python3.6+selenium3.0.2+Firefox50.0+win7 [定位方法] 1.方法:find_element_by_css_selector('xx') CSS的语法比 ...

  10. 系统A一定会按照自我的样子改造世界

    A一定会按照自己的样子去构建系统A1,A1一定还会按照自己的样子去构建系统A1.1,A1.1一定还是会按照自我的样子去构建A1.1.1……我们编程,我们改造世界,我们的方向是被注定要朝着构建人造人的方 ...