I am trying to add a file upload in web page
html
<input type="file" class="form-control" name="doxFile" id="dox_dox"/>
<button class="btn btn-default" onclick="Uptest();">Upload</button>
js
function Uptest()
{
var file = $("#dox_dox")[0].files[0];
var formData = new FormData();
var reader = new FileReader();
reader.onload = function(){
var dataURL = reader.result;
formData.append('doxFile', dataURL);
formData.append('name', file.name);
formData.append('type', file.type);
$.ajax(
{
url : '/savedox/',
type: "POST",
data : formData,
dataType:'json',
cache: false,
processData: false,
contentType: false,
success: function(data, textStatus, jqXHR){
console.log('Saved')
}})
}
reader.readAsDataURL(file);
py
@http.route('/savedox/', csrf=False, auth='public', method=['POST', 'GET'])
def save_dox(self, **kw):
# cgitb.enable()
data = kw.get('doxFile')
file_name = kw.get('name')
file_size = kw.get('size')
file_type = kw.get('type')
local_context = dict(request.env.context, force_company = request.env.user.company_id.id,company_id =request.env.user.company_id.id)
sam = request.env['ir.attachment'].new({'name':file_name,'db_datas':data,'datas_fname':file_name,
'type':'binary','file_size':file_size,'mimetype':file_type
})
samo = sam._convert_to_write({name: sam[name] for name in sam._cache})
new_sam = request.env['ir.attachment'].with_context(local_context).sudo().create(samo)
what happing is it add the file to the ir.attachemt without errors and when I am trying to download the file it work only if the file is 'txt or zip' the other file type like (.jpg,pdf,xls) it downloads but when I open it it's in bad form like if there is encoding problem,
any help,
also in the py I tried "datas" instead of "db_datas"