Odoo Help

0

Add Attachement From website.

By
Samer Al-Sarhan
on 5/19/17, 12:22 PM 765 views

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"

Samer Al-Sarhan
on 5/20/17, 2:54 AM
0

Jainesh Shah

--Jainesh Shah--
1339
| 5 2 7
Ahmedabad, India
--Jainesh Shah--


Jainesh Shah
On 1/19/18, 12:52 AM

Hi samer al- sarhan,

You can try using this code:

Xml code :

<template id="detail" name="Project Detail">
       <t t-call="website.layout">
           <div class="col-md-offset-5 col-sm-offset-4 col-sm-8 col-md-7" style="margin-top:30px;">
               <form action="/project/uploaded" method="post" class="s_website_form form-horizontal container-fluid mt32" enctype="multipart/form-data" id="file_upload_form">
                   <div t-attf-class="form-group">
                       <div class="col-md-7 col-sm-8">
                           <input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>
                           <input type="hidden" name="project_id" t-att-value="project.id"/>
                           <input type="file" name="attachment" class="file" multiple="true" data-show-upload="true" data-show-caption="true" lass="file" data-show-preview="true" id="project.id"/>
                           <button type="submit" name="Upload" style="margin-top:20px;" class="fa fa-upload">
                               Upload
                           </button>
                       </div>
                   </div>
               </form>
           </div>
       </t>
   </template>

.py code :

@http.route('/project/uploaded', type='http', auth="public", website=True)
   def upload_files(self, **post):
       values = {}
       if post.get('attachment',False):
           Attachments = request.env['ir.attachment']
           name = post.get('attachment').filename      
           file = post.get('attachment')
           project_id = post.get('project_id')
           attachment = file.read()
           attachment_id = Attachments.sudo().create({
               'name':name,
               'datas_fname': name,
               'res_name': name,
               'type': 'binary',   
               'res_model': 'model.model,
               'res_id': project_id,
               'datas': attachment.encode('base64'),
           })
           value = {
               'attachment' : attachment_id
           }
            return request.render("modulename.template_to_render", value)

Hope this will help you.

Thanks.

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)

Stats

Asked: 5/19/17, 12:22 PM
Seen: 765 times
Last updated: 1/19/18, 12:52 AM