在SAE上使用Ueditor的图片上传功能
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的图片上传功能的更多相关文章
- uEditor独立图片上传
项目中.上传图片,非常希望有一款比较兼容的查件. 网上找了一些,图片上传立刻显示的js代码,还有uploadify.都会碰到这样那样的不兼容和其它头疼的问题. 后来想,干脆就用php的上传类最干脆.但 ...
- springboot整合ueditor实现图片上传和文件上传功能
springboot整合ueditor实现图片上传和文件上传功能 写在前面: 在阅读本篇之前,请先按照我的这篇随笔完成对ueditor的前期配置工作: springboot+layui 整合百度富文本 ...
- 有关于weiphp2.00611上传sae的一些注意(图片上传解决方案)
一.安装中注意的事项 安装时使用的系统为weiphp2.0611 版本 1.将所有文件上传到代码库中 2.按照步骤进行安装weiphp,注意在数据库导入的时候需要手动导入. ...
- 对百度的UEditor多图片上传的一些补充
我已经写了一篇文章关于百度的UEditor提取多图片上传模块.如果还没有看过,请点击以下链接查看 http://www.cnblogs.com/luke1006/p/3719029.html 出差了两 ...
- 部署新浪SAE web.py Session及图片上传等问题注意事项
1.以下几条代码解决编码问题 import sysreload(sys)sys.setdefaultencoding('utf-8') 2.图片上传问题 需要开通sina的Storage服务,随便建个 ...
- 单独调用Ueditor的图片上传功能
<!DOCTYPE html> <html> <head> <title></title> <script src="/sc ...
- asp.net 百度编辑器 UEditor 上传图片 图片上传配置 编辑器配置 网络连接错误,请检查配置后重试
1.配置ueditor/editor_config.js文件,将 //图片上传配置区 ,imageUrl:URL+"net/imageUp.ashx" //图片上传提交地址 ,im ...
- UEditor使用------图片上传与springMVC集成 完整实例
UEditor是一个很强大的在线编辑软件 ,首先讲一下 基本的配置使用 ,如果已经会的同学可以直接跳过此节 ,今天篇文章重点说图片上传; 一 富文本的初始化使用: 1 首先将UEditor从官网下载 ...
- 百度ueditor的图片上传,前后端交互使用
百度ueditor的使用 一个文本编辑器,看了网上很多文档写的很乱,这里拾人牙慧,整理下怎么使用. 这个东西如果不涉及到图片附件上传,其实很简单,就是几个前端文件,直接引用,然后配置下ueditor. ...
随机推荐
- 【NOIP 2011】 计算系数
[题目链接] https://www.luogu.org/problemnew/show/P1313 [算法] 二项式定理 [代码] #include<bits/stdc++.h> usi ...
- mfs使用指引
客户端工具集 mfsgetgoal #设定副本数 mfssetgoal #获取副本数 mfscopygoal # mfsgetsclass mfssetsclass mfscopysclass mfs ...
- php mktime和strtotime
本文章来给各位同学介绍一下利用php用strtotime或mktime指定日期数据(本周,上周,本月,上月,本季度)实例,希望对各位同学会有所帮助呀. strtotime定义和用法 strtotime ...
- C - Queue at the School
Problem description During the break the schoolchildren, boys and girls, formed a queue of n people ...
- C++中内存分配、函数调用和返回值问题
转载博客:http://blog.csdn.net/q_l_s/article/details/52176159(源地址找不到,就贴了这位大神的博客地址,他也是转载的,不过要是学习的话,他的博客很不错 ...
- JQuery学习笔记系列(一)----选择器详解
笔者好长时间没有更新过博客园的笔记了,一部分原因是去年刚刚开始工作一段时间忙碌的加班,体会了一种每天加班到凌晨的充实感,之后闲暇时间了也因为自己懒惰没有坚持记笔记的习惯,现在重新拾起来. 借用古人的一 ...
- PostgreSQL导出表中数据
下边的步骤详细讲述了从Postgres数据库中导出数据的方法: (1)将PostgreSQL数据库的psql工具所在的路径添加到系统的环境变量中:(2)运行cmd,在窗口中输入psql,会有提示输入口 ...
- 洛谷P3113 [USACO14DEC]马拉松赛跑Marathon_Gold 线段树维护区间最大值 模板
如此之裸- Code: #include<cstdio> #include<cstring> #include<cmath> #include<algorit ...
- python的jieba分词
# 官方例程 # encoding=utf-8 import jieba seg_list = jieba.cut("我来到北京清华大学", cut_all=True) print ...
- HDU1241 - Oil Deposits【DFS】
GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块.他们通过专业设备,来分析每个小块中是否蕴藏石油.如果这些蕴藏石 ...