1

I have a webpage where the user can upload a PDF file and send it using AJAX to my Flask application. Here is the ajax code:

var formData = new FormData();
formData.append('attachment', document.getElementById("attachment").files[0]);
$.ajax({
    type: 'POST',
    url: 'process_award_storage',
    contentType: false,
    processData: false,
    data: formData
})

I do not think that this has any problems because I can print out the content and title of file in the python code. Then my flask model is defined as such, using LargeBinary for the PDF attachment:

class AwardStore(db.Model):
    __tablename__ = 'awards_store'
    id = db.Column(db.Integer, primary_key=True)
    ...
    file = db.Column(db.LargeBinary, nullable=True) #I am sure this is the right file type for PDF saving

Lastly, here is how the AJAX file is saved to the database:

     award = AwardStore(name=name, file=request.files['attachment'].read())
     db.session.add(award)
     db.session.commit()

I can see using my MySQL Workbench that it is saved. However, when I try to download the BLOB from there to the Desktop and open it, it says "Failed to load PDF document". The same happens when I use flask's send_file. It seems like I did everything as I saw online, but something is wrong.

Maybe these warnings are related?:

C:\Users\msolonko\Desktop\NICKFI~1\CACHTM~1\APP_DE~1\virt\lib\site-packages\pymysql\cursors.py:170: Warning: (1300, "Invalid utf8 character string: 'C4E5F2'")
  result = self._query(query)
C:\Users\msolonko\Desktop\NICKFI~1\CACHTM~1\APP_DE~1\virt\lib\site-packages\pymysql\cursors.py:170: Warning: (1265, "Data truncated for column 'file' at row 1")
  result = self._query(query)

I tried googling them and did not find anything. I appreciate any assistance.

EDIT:

I noticed that small files are typically uploaded and displayed properly. The issue is with files with sizes > ~50kB. The database I am using the AWS RDS, Is there a setting I can change somewhere to enable greater sizes?

shurup
  • 751
  • 10
  • 33

1 Answers1

1

The LargeBinary accepts a length field also. which converts to mysql's BLOB whose default length is 65535 bytes (64kb). Try increasing the length and set it to

a MEDIUMBLOB for 16777215 bytes (16 MB)

a LONGBLOB for 4294967295 bytes (4 GB).

Hope this will help

thedudecodes
  • 1,479
  • 1
  • 16
  • 37