How to set validation for document upload only to .pdf in openERP
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Kế toán
- Tồn kho
- PoS
- Project
- MRP
Câu hỏi này đã bị gắn cờ
Hello @rumaan Try This:
from odoo.exceptions import UserError
import mimetypes
from odoo.tools.mimetypes import guess_mimetype
file_name = fields.Char('File Name')
mimetype = None
if mimetype is None and self.file_name:
mimetype = mimetypes.guess_type(res.file_name)[0]
if not mimetype == 'pdf':
raise UserError('Allowed Format Pdf')
I can't answer directly to Jaiswal Shivam, but their answer is dangerous from a security standpoint. The attacker controls the file name and thus the validation can be bypassed by setting the expected extension (malicious_content.xlsx) but the content can be anything (e.g. php script).
The accepted answer using mimetype guessing is much better from a security standpoint.
Or you can also try this code::
# create a char field
file_data = fields.Binary('File')
file_name = fields.Char('File Name')
# Go to xml and
def check_file_type(self):
if not self.file_name.endswith('.xlsx'): //Here you can define your file format like .pdf, .word etc.//
raise UserError('Please insert a valid file')
Thanks!!
Were you able to solve it?
Yes..
Bạn có hứng thú với cuộc thảo luận không? Đừng chỉ đọc, hãy tham gia nhé!
Tạo tài khoản ngay hôm nay để tận hưởng các tính năng độc đáo và tham gia cộng đồng tuyệt vời của chúng tôi!
Đăng ký