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. php添加文件到压缩文件夹

    extension=php_zip.dll注释掉 <?php$filename = "test.zip"; ob_end_clean(); $zip = new ZipArc ...

  2. 认识React框架

    在大厂面试的时候被问会不会React框架几乎是必须的,可见React框架在现在前端市场的份额.所以说学习React框架的必要性. react框架起源于Facebook的内部项目,因为对市场上的Java ...

  3. 框架,表格,表单元素,css基础以及基本标签的结合

    <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  4. html/css常用合集

    1. 消除inline-block元素间的换行间隙问题: {font-size:0;} 兼容IE6/7浏览器的方法:letter-spacing属性.   2.让两个inline-block的div顶 ...

  5. 【Oracle】Rman简介

    1.备份和恢复功能的用途: –保护数据 -介质故障 -用户错误 -应用程序错误 –数据保存和历史数据保留 –数据传输 2.典型的备份和恢复任务 –通过配置数据库实现可恢复 –定义备份计划 –对不同类型 ...

  6. 使用一行代码解决IE浏览器兼容问题

    在网站开发中不免因为各种兼容问题苦恼,针对兼容问题,其实IE给出了解决方案Google也给出了解决方案 百度也应用了这种方案去解决IE的兼容问题 百度源代码如下 <!Doctype html&g ...

  7. [Intermediate Algorithm] - Sum All Odd Fibonacci Numbers

    题目 给一个正整数num,返回小于或等于num的斐波纳契奇数之和. 斐波纳契数列中的前几个数字是 1.1.2.3.5 和 8,随后的每一个数字都是前两个数字之和. 例如,sumFibs(4)应该返回 ...

  8. JA document的练习

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

  9. JS 从100里面随机取10个数比大小

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

  10. 路飞学城Python-Day96

    51-数据库表关系之一对多 为什么有多表的概念? 一个Book有单表的时候只能查询Book表中的内容,要想查询更多的出版社的信息,需要再将单表的内容不断更新 导致单表会存储大量的重复的信息,浪费了大量 ...