[ADD] Binary image working under chrome
bzr revid: fme@openerp.com-20110519101726-valkxw6v6zpwbpyi
This commit is contained in:
parent
8799889838
commit
665e779b78
|
@ -571,7 +571,13 @@ class FormView(View):
|
||||||
return open(os.path.join(openerpweb.path_addons, 'base', 'static', 'src', 'img', 'placeholder.png'), 'rb').read()
|
return open(os.path.join(openerpweb.path_addons, 'base', 'static', 'src', 'img', 'placeholder.png'), 'rb').read()
|
||||||
|
|
||||||
@openerpweb.httprequest
|
@openerpweb.httprequest
|
||||||
def upload(self, request, session_id, callback, ufile):
|
def upload(self, request, session_id, callback, ufile=None):
|
||||||
|
cherrypy.response.timeout = 500
|
||||||
|
headers = {}
|
||||||
|
for key, val in cherrypy.request.headers.iteritems():
|
||||||
|
headers[key.lower()] = val
|
||||||
|
size = int(headers.get('content-length', 0))
|
||||||
|
# TODO: might be usefull to have a configuration flag for max-lenght file uploads
|
||||||
try:
|
try:
|
||||||
out = """<script language="javascript" type="text/javascript">
|
out = """<script language="javascript" type="text/javascript">
|
||||||
var win = window.top.window,
|
var win = window.top.window,
|
||||||
|
@ -585,18 +591,11 @@ class FormView(View):
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
</script>"""
|
</script>"""
|
||||||
size = 0
|
data = ufile.file.read()
|
||||||
while True:
|
args = [size, ufile.filename, ufile.headers.getheader('Content-Type'), base64.encodestring(data)]
|
||||||
data = ufile.file.read(8192)
|
|
||||||
if not data:
|
|
||||||
break
|
|
||||||
size += len(data)
|
|
||||||
filename = ufile.filename
|
|
||||||
# TODO: write file to tmp file
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
size = False
|
args = [False, e.message]
|
||||||
filename = e.message
|
return out % (simplejson.dumps(callback), simplejson.dumps(args))
|
||||||
return out % (simplejson.dumps(callback), simplejson.dumps([size, filename, ufile.headers.getheader('Content-Type')]))
|
|
||||||
|
|
||||||
class ListView(View):
|
class ListView(View):
|
||||||
_cp_path = "/base/listview"
|
_cp_path = "/base/listview"
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
/* TODO: separate openerp web client page css from openerp views css */
|
/* TODO: separate openerp web client page css from openerp views css */
|
||||||
|
|
||||||
body {
|
body {
|
||||||
padding: 0;
|
padding: 0;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
|
|
|
@ -1200,26 +1200,34 @@ openerp.base.form.FieldImage = openerp.base.form.Field.extend({
|
||||||
},
|
},
|
||||||
set_value_from_ui: function() {
|
set_value_from_ui: function() {
|
||||||
},
|
},
|
||||||
|
set_image_maxwidth: function() {
|
||||||
|
this.$element.find('img.oe-binary-image').css('max-width', this.$element.width());
|
||||||
|
},
|
||||||
on_file_change: function() {
|
on_file_change: function() {
|
||||||
|
this.set_image_maxwidth();
|
||||||
|
// TODO: on modern browsers, we could directly read the file locally on client ready to be used on image cropper
|
||||||
|
// http://www.html5rocks.com/tutorials/file/dndfiles/
|
||||||
|
// http://deepliquid.com/projects/Jcrop/demos.php?demo=handler
|
||||||
window[this.iframe] = this.on_file_uploaded;
|
window[this.iframe] = this.on_file_uploaded;
|
||||||
this.$element.find('form.oe-binary-form input[name=session_id]').val(this.session.session_id);
|
this.$element.find('form.oe-binary-form input[name=session_id]').val(this.session.session_id);
|
||||||
this.$element.find('form.oe-binary-form').submit();
|
this.$element.find('form.oe-binary-form').submit();
|
||||||
this.toggle_throbbler();
|
this.toggle_throbber();
|
||||||
},
|
},
|
||||||
toggle_throbbler: function() {
|
toggle_throbber: function() {
|
||||||
this.$element.find('div.oe-binary-progress, div.oe-binary-image-buttons').toggle();
|
this.$element.find('div.oe-binary-progress, div.oe-binary-image-buttons').toggle();
|
||||||
},
|
},
|
||||||
on_file_uploaded: function(size, name, content_type) {
|
on_file_uploaded: function(size, name, content_type, img) {
|
||||||
delete(window[this.iframe]);
|
delete(window[this.iframe]);
|
||||||
if (size === false) {
|
if (size === false) {
|
||||||
this.notification.warn("File Upload", "There was a problem while uploading your file");
|
this.notification.warn("File Upload", "There was a problem while uploading your file");
|
||||||
// TODO: use openerp web exception handler
|
// TODO: use openerp web exception handler
|
||||||
console.log("Error while uploading file : ", name);
|
console.log("Error while uploading file : ", name);
|
||||||
} else {
|
} else {
|
||||||
alert('File uploaded')
|
this.value = img;
|
||||||
console.log("Size", size, "Name", name, "Content", content_type);
|
this.$element.find('img.oe-binary-image').attr('src', 'data:' + (content_type || 'image/png') + ';base64,' + img);
|
||||||
|
this.on_ui_change();
|
||||||
}
|
}
|
||||||
this.toggle_throbbler();
|
this.toggle_throbber();
|
||||||
},
|
},
|
||||||
on_clear: function() {
|
on_clear: function() {
|
||||||
if (this.value !== false) {
|
if (this.value !== false) {
|
||||||
|
@ -1227,9 +1235,11 @@ openerp.base.form.FieldImage = openerp.base.form.Field.extend({
|
||||||
this.$element.find('img.oe-binary-image').attr('src', '/base/static/src/img/placeholder.png');
|
this.$element.find('img.oe-binary-image').attr('src', '/base/static/src/img/placeholder.png');
|
||||||
this.on_ui_change();
|
this.on_ui_change();
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
},
|
},
|
||||||
set_value: function(value) {
|
set_value: function(value) {
|
||||||
this._super.apply(this, arguments);
|
this._super.apply(this, arguments);
|
||||||
|
this.set_image_maxwidth();
|
||||||
var url = '/base/formview/image?session_id=' + this.session.session_id + '&model=' +
|
var url = '/base/formview/image?session_id=' + this.session.session_id + '&model=' +
|
||||||
this.view.dataset.model +'&id=' + (this.view.datarecord.id || '') + '&field=' + this.name + '&t=' + (new Date().getTime())
|
this.view.dataset.model +'&id=' + (this.view.datarecord.id || '') + '&field=' + this.name + '&t=' + (new Date().getTime())
|
||||||
this.$element.find('img.oe-binary-image').attr('src', url);
|
this.$element.find('img.oe-binary-image').attr('src', url);
|
||||||
|
|
|
@ -420,7 +420,8 @@
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="oe-binary-progress" style="display: none">
|
<div class="oe-binary-progress" style="display: none">
|
||||||
Uploading ...
|
<img src="/base/static/src/img/throbber.gif" width="16" height="16"/>
|
||||||
|
<b>Uploading ...</b>
|
||||||
</div>
|
</div>
|
||||||
<iframe t-att-id="widget.iframe" t-att-name="widget.iframe" style="display: none"> </iframe>
|
<iframe t-att-id="widget.iframe" t-att-name="widget.iframe" style="display: none"> </iframe>
|
||||||
</td>
|
</td>
|
||||||
|
|
Loading…
Reference in New Issue