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. 前端分页功能实现(PC)

    <!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>加 ...

  2. Android 解决toolbar标题不显示问题

    问题原因:toolbar的兼容性有问题 解决办法: setSupportActionBar(toolbar); toolbar使用步骤: 1.编写menu.xml 为了保持兼容需要这样写: andro ...

  3. hdu 2768 Cat vs. Dog 最大独立集 巧妙的建图

    题目分析: 一个人要不是爱狗讨厌猫的人,要不就是爱猫讨厌狗的人.一个人喜欢的动物如果离开,那么他也将离开.问最多留下多少人. 思路: 爱猫和爱狗的人是两个独立的集合.若两个人喜欢和讨厌的动物是一样的, ...

  4. 关于出现Failed to instantiate SLF4J LoggerFactory问题原因,解决办法

    在创建spring boot 文档进行配置的时候,因为使用spring boot 父级依赖的版本 <artifactId>spring-boot-starter-parent</ar ...

  5. 在MyEclipse中使用debug模式

     转:http://blog.csdn.net/competerh_programing/article/details/6773371 1, 首先在一个java文件中设断点,然后运行,当程序走到断点 ...

  6. 百度map API

    1.做demo用的 http://developer.baidu.com/map/jsdemo.htm demo代码(外部使用的话需要提供密钥): <!DOCTYPE html> < ...

  7. PostgreSQL 保留关键字添加方法之一,不带参数的函数

    以添加sysdate关键字为例说明: 1.src\backend\parser\gram.y文件的%token <keyword>段添加SYSDATE关键字,建议按照ASCII顺序添加 2 ...

  8. root密码忘记怎么办?

    忘记root密码:按 e进入内核在按e,后面加1 .按b启动 进入命令行输入passwd,设置新的密码后exit退出即可

  9. OpenCV中GPU模块使用

    CUDA IT168的文章系列: Cuda的初始化:http://tech.it168.com/a2011/0715/1218/000001218458.shtml OpenCV: OpenCV中GP ...

  10. 项目随笔@Service("testService")-------第二篇

    在springmvc中使用注解已经司空见惯了,今天见到了@Service("xxx")这种形式,让我大吃一惊.原来在service后面可以加参数,作为该service的名字,在sp ...