0

i wanted to fetch the data(blob) to download it from my database. I'm facing some error with bytes TypeError: a bytes-like object is required, not 'int'. I had been working on this for so long.

@app.route("/search", methods=['GET','POST'])
def search():

    if request.method == "POST":

        if not request.form.get("search"):

            return error("error")

        conn= sqlite3.connect("YTD2.db")
        cursor = conn.cursor()
        c = cursor.execute("SELECT * FROM my_table")

        id = db.execute("SELECT id FROM my_table WHERE name=:name", \
                        name=request.form.get("search"))
        # print(data)
        for sub in id:
            for key in sub:
                sub[key] = int(sub[key])

        for x in c.fetchall():
            data_v=x[1][sub[key]]
            break
        
        conn.commit()
        cursor.close()
        conn.close()

        # print(c)    
        return send_file(BytesIO(data_v), attachment_filename='download.pdf', as_attachment=True)
    
    return redirect("home.html")

here is the error

DEBUG:cs50:SELECT id FROM my_table WHERE name='网格.pdf'
ERROR:app:Exception on /search [POST]
Traceback (most recent call last):
  File "e:\programme\python\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "e:\programme\python\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "e:\programme\python\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "e:\programme\python\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "e:\programme\python\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "e:\programme\python\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\Win X\Desktop\CS50 FP\TRY\Download Prob\app.py", line 176, in search
    return send_file(BytesIO(data_v), attachment_filename='download.jpg', as_attachment=True)
TypeError: a bytes-like object is required, not 'int'
INFO:werkzeug:127.0.0.1 - - [27/Jun/2020 23:44:12] "POST /search HTTP/1.1" 500 -

anyone else have some good algorithm to flask blob from database and store as PDF, i had been working on this using flask_wtf

here this is the new update that how i store my blob file

@app.route('/upload', methods=["GET", "POST"])
@login_required
def upload():

    form = UploadForm()
    if request.method == "POST":
       
        if form.validate_on_submit():
            
            file_name = form.file.data
            database(name=file_name.filename, data=file_name.read())

            return render_template("upload.html", form=form)       
    return render_template("upload.html", form=form)

class UploadForm(Form):
    file = FileField()
    submit = SubmitField("submit")
    download = SubmitField("download")

def database(name, data):
    db.execute("INSERT INTO my_table(name, data) VALUES (?,?)",(name,data))
  • BytesIO requires a 'bytes' object but 'data_v' is evidently an integer. Do you really need BytesIO? If so, you could pass bytes(data_v) instead of data_v to BytesIO – MLguy Jun 27 '20 at 16:07
  • That might indicate a bug in how data_v is generated too. Have you checked it's the right data in addition to it being the right type? If you enumerate or index a bytes object you'll get an integer -- e.g. `b'a'[0] == 97`. – Hans Musgrave Jun 27 '20 at 16:10
  • yesyesyes, i found out this when i print(data_v), any solution for this, i kinda blur – Tee Xian Wei Jun 27 '20 at 16:20
  • Please provide a [mcve]. What do you understand from that error message? – AMC Jun 27 '20 at 16:27
  • maybe this https://stackoverflow.com/questions/21017698/converting-int-to-bytes-in-python-3 could bring a hint to you (casting `int` to `bytes`) – cizario Jun 27 '20 at 16:42
  • how you even store files as `blob` in your database ? would you mind showing us that particular code ? – cizario Jun 27 '20 at 16:42
  • i had update the code that how i store blob file – Tee Xian Wei Jun 27 '20 at 23:52

0 Answers0