Python Forum
Uploading multiple pdfs to Flask ( Sql alchmey) with additional variables
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Uploading multiple pdfs to Flask ( Sql alchmey) with additional variables
#1
i have tried to explain this in a helpful clean way but it is quite long, so get snacks before starting ( id recommend jelly babies for that energy burst), ive tried to break the problem down into: desired outcome, what i have, where i think the solution lies, what i need help with.

My language: Before i start when i mention the variable 'doctype' i mean a category in which you might class a document by, e.g. user manual.
When i mention filename/filetype i mean the type of file it is e.g. '.pdf'. or the filename: 'python.pdf.'
Just dodging that question before it is asked :P.

Desired outcome:

Files are to be uploaded to a sqlalchemy database table. What I would like is for a user to be able to upload multiple files at 'the same time,'(loosely as i dont think the same time is strictly possible, more like one at a time but selecting multiple from the file input.) Linking variables, product_serial_number and doctype to the file in columns on the same row as the file.

So sql table would look something like:

filename data product_serial_number doctype


What i have so far:

Currently i have the html which will all multiple file selection:
<form method=POST enctype=multipart/form-data action='/upload_pdf'>
   <input type='file' name='inputFile' multiple="multiple" class='mp_FileInput'></input>
   <button class='pdf01_upload_button' type='submit' value='Upload' ><b>Upload</b></button>
</form>
html which uses JSON to allow a user to select a doctype and serial number:

<p> serial_number: <input type='text' id='serial_number_entry' name='serial_number_entry'></p>
<select type='text' name='doctype_entry' id='doctype_entry' placeholder='Select a Doc Type...'>
    <option value='' disabled selected>Select a Doc Type...</option>
    <option>Build Log</option>
    <option>Calibration Certificate</option>
    <option>Factory Acceptance Test</option>
    <option>Performance Verification Certificate</option>
    <option>Procedure Checklist</option>
    <option>System Configuration Tracking</option>
    <option>Test Certificate</option>
</select>
<button id="add-pdf">Upload PDF</button>
<p id='result'> ... </p>

<script>
  $(function() {
    $('#add-pdf').on('click', function() {
      $.getJSON('{{ url_for('test_domain.test_process') }}',{
        serial_number: $('input[name="serial_number_entry"]').val(),
        doctype: $('select[name="doctype_entry"]').val(),
      }, function(data) {
        $("#result").text(data.result);
      });
      return false;
    });
  });
</script>
And the python for a single pdf upload ( ive striped down to the bare bones so if there is a random 'else' or 'if' then its not a codding mistake, just me cutting things out.):
def upload_file_pdf():
    product_serial_number_store = request.form['product_serial_number']
    Doc_Type_Store = request.form['Doc_Type_Input']

    if Tempfile and allowed_file(Tempfile.filename):
      filename = secure_filename(Tempfile.filename)
      newFile = pdf_test_equipment_store(related_to=related_to, name=Tempfile.filename,  product_serial_number= product_serial_number_store,doctype=Doc_Type_Store, date_time=datetime.utcnow(), data=Tempfile.read(), place_of_procedure=Tester.location, uploaded_by=Tester.username)
      db.session.add(newFile)
      db.session.commit()
      flash(' Saved ' + Doc_Type_Store + ' for ' + product_serial_number_store + ' to the test equipment pdf store!')
      return render_template('upload_pdf.html', title='upload_pdf', filename=filename)
               
And i can combine both html documents in 1 form, lose the javascript and use the python in order to allow for 1 a single pdf to be uploaded.

What needs to happen:
User clicks upload button and selects ( lets say 10) pdfs to upload.
I would like my website to select the first pdf of the list, ask the user to input a doctype and product_serial_number , then query the information and upload to table. Then select pdf 2 from the list and ask the user to input doctype and product_serial_number. And repeat.

What i need help with.

So i can do the script for 1 pdf. and i assume by using the JSON and the use of a prompt box i can loop the script and just get the next file in the list. I need help implementing my thoughts though, or being corrected if my theory is incorrect.

To anyone that is still reading, thanks for sticking with me and hope i have explained in enough detail that you might be able to help me.

I have just put together a script ( shown below) which uploads multiple files to a database, so now I only need help with linking the the doctype and product_serial_number to each pdf.
if request.method == 'POST':
   uploaded_files =request.files.getlist("file[]")
   print uploaded_files
   for file in uploaded_files:
       filename = secure_filename(file.filename)
       newFile = pdf_test_equipment_store(name=file.filename, date_time=datetime.utcnow(), data=file.read(), place_of_procedure=Tester.location, uploaded_by=Tester.username)
       db.session.add(newFile)
       db.session.commit()

   flash(' Saved to the pdf store!')
   return render_template('upload_pdf.html', title='upload_pdf', filename=filename)
else:
   return render_template('upload_pdf.html', title='upload_pdf')
Reply


Messages In This Thread
Uploading multiple pdfs to Flask ( Sql alchmey) with additional variables - by KirkmanJ - Jul-18-2018, 08:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to pass javascript variables to url_for function in a flask template experimental 5 6,455 Oct-29-2020, 03:29 AM
Last Post: universe
  Uploading images from multipart/data/-from get corrupted jdc18 0 1,959 Sep-30-2020, 07:16 PM
Last Post: jdc18
  Flask, Posgresql - Multiple requests are not working bmaganti 5 2,801 Feb-20-2020, 03:02 PM
Last Post: bmaganti
  How to create Flask-Stripe Checkout and Charge for the multiple items Antares 4 5,305 Jul-05-2019, 10:20 AM
Last Post: Antares
  display multiple sensors on webpage python flask jinja pascale 6 5,339 Jan-29-2019, 10:10 AM
Last Post: pascale
  How to upload images after first paragraph ends instead of uploading them at the end? SamLearnsPython 0 1,970 Sep-15-2018, 06:53 AM
Last Post: SamLearnsPython
  [Flask] Uploading CSV file to flask, only first line being uploaded. Help ! KirkmanJ 2 6,830 Jun-25-2018, 02:24 PM
Last Post: KirkmanJ

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020