Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Tempfile Required
#1
I plan to use tempfile to read a pdf file and store it. Then open the file in a DIFFERENT function and upload it to a flask table as a BLOB.

I currently have the following code which I THINK would upload multiple files to the tempfile 'storage', ( This is untested so I do not know if the files would just overwrite each over, and thus when I go to read them there would only be 1 )

def 1 
... 

for file in uploaded_files:
   if allowed_file(file.filename):
       temp_filename = secure_filename(file.filename)
       filenames.append(temp_filename)
       my_temp_file=tempfile.NamedTemporaryFile(delete=False)
       file.save(my_temp_file)
       file.temp_name=my_temp_file.name
...
I then want to access the tempfile 'storage' in a 2nd function. Something along the lines of:
def 2 
...
for name in filenames:
    actual_file=open(temp.name, 'rb')
I had been previously using globals to do this but figured out that my database will be used by multiple people, which of cause means globals are a huge no no.
        
for file in pdf_values:
   actual_file=open(file.temp_name, 'rb')
Where pdf_values is a global set by : set_pdf_values(uploaded_files) .

I do not know if the tempfile method is the correct way of solving this issue, I assume i am missing an import in the 2nd function, or a specific way of opening the tempfiles. Anyone who has an idea on how to solve this error or can point me to helpful documentation with examples I would be very grateful.

Jack
Reply
#2
(Aug-23-2018, 09:08 AM)KirkmanJ Wrote: Anyone who has an idea on how to solve this error
Can you post the error?
Reply
#3
Sorry its been a while, been really busy.

So i have troubleshooted this error a little and these are my findings:

If I use the code:
for name in filenames:
    actual_file=open(name.temp_name, 'rb')
This is the error message shown:

actual_file=open(name.temp_name, 'rb')
AttributeError: 'str' object has no attribute 'temp_name'

If I try and save uploaded_files as a session variable to avoid the use of globals with the following code:

def 1 
...
session['uploaded_files'] = uploaded_files
...
I get the error message:
TypeError: <FileStorage: u'1.3MB.pdf' ('application/pdf')> is not JSON serializable

If I try to upload my_temp_files as a session vaiable:
session['my_temp_file'] =my_temp_file
I get the same error message as above:
TypeError: <FileStorage: u'1.3MB.pdf' ('application/pdf')> is not JSON serializable

I have also thought about using the 'send_file' flask command but do not know if this is a possibility.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error on open of file created with tempfile.TemporaryDirectory() Brian177 4 6,144 Apr-05-2021, 07:12 PM
Last Post: Brian177
  Create tempfile that use in loop for insert into DB Firsttimepython 2 2,089 May-29-2020, 04:15 PM
Last Post: Firsttimepython
  VideoWriter unreadable output using tempfile fourtino2322 1 2,201 Mar-27-2020, 03:15 PM
Last Post: fourtino2322

Forum Jump:

User Panel Messages

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