Code Point
September 08, 2010, 08:28:41 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Geshi Sintax Hilighting Mod Installed. Tens of languages available.
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Uploading binary file not working  (Read 4841 times)
ecs1749
Jr. Member
*

Karma: 0
Offline Offline

Posts: 10


« on: October 15, 2006, 11:43:50 PM »

I followed the example at:

http://webpython.codepoint.net/file_upload

and it works for text file.  I changed the open statement to do "wb" and tried to transfer a jpeg file.  After the transfer, the system no longer recognizes the file as a jpeg file.

Any thoughts?

Thanks,
Logged
CPN
Administrator
Jr. Member
*****

Karma: 0
Offline Offline

Posts: 11


WWW
« Reply #1 on: October 16, 2006, 12:24:50 PM »

Windows needs special care for binary files:

Code
try: # Windows needs stdio set for binary mode.
   import msvcrt
   msvcrt.setmode (0, os.O_BINARY) # stdin  = 0
   msvcrt.setmode (1, os.O_BINARY) # stdout = 1
except ImportError:
   pass

I have updated the examples in the tutorial.
Logged
ecs1749
Jr. Member
*

Karma: 0
Offline Offline

Posts: 10


« Reply #2 on: October 16, 2006, 04:16:24 PM »

Works now!

Thanks,
Logged
ecs1749
Jr. Member
*

Karma: 0
Offline Offline

Posts: 10


« Reply #3 on: October 16, 2006, 04:20:41 PM »

As long as you're updating the example, I believe the File Upload example is missing a file close statement.  The file never gets closed otherwise.

Regards,
Logged
CPN
Administrator
Jr. Member
*****

Karma: 0
Offline Offline

Posts: 11


WWW
« Reply #4 on: October 16, 2006, 05:12:22 PM »

The returned file object is not kept. It is immediately consumed thus there is nothing to be closed:

Code
   open('files/' + fn, 'wb', 10000).write(fileitem.file.read())
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!