2009-08-12 2 views
0

dies ist mein Testcode:Unit-Test über Upload xls-Datei

def test_import_data(self): 
     f = open('commend/fixtures/Book2.xls') 
     postdata = {'datatype':'intonetwork','datafile':f} 
     response = self.client.post('/commend/saledata/import_data/',postdata) 
     self.failUnlessEqual(response.status_code, 200) 

aber in der Ansicht Code:

file = request.FILES['datafile'] 
size = file.size 

die Größe nur gleich 6.

so debuggen ich den Client .Post Code:

def encode_file(boundary, key, file): 
    to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET) 
    return [ 
     '--' + boundary, 
     'Content-Disposition: form-data; name="%s"; filename="%s"' \ 
      % (to_str(key), to_str(os.path.basename(file.name))), 
     'Content-Type: application/octet-stream', 
     '', 
     file.read() 
    ] 

wenn ich die Komm. öffne Ende/Einbauten/Book2.xls.

>>> f = open("commend/fixtures/Book2.xls") 
>>> f.read() 

'\ xd0 \ XCF \ x11 \ xe0 \ XA1 \ xb1'

>>> f.read() 

‚\ x00 \ XB9 \ XA4 \ XD7 \ XF7 \ xb1 \ festen \ x00 \ x03 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x0 0 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x0 0 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x0 0 \ x00 \ x00 \ x00 \ x00 \ x01 \ x00 \ xfe \ xff \ x03 \ n \ x00 \ x00 \ xff \ xff \ xff \ xff \ x08 \ x02 \ x00 \ x00 \ x00 \ x00 \ x00 \ xc0 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00F # \ x00 \ x00 \ x00Microsoft Office Excel 200 3 \ xb9 \ xa4 \ xd7 \ xf7 \ xb1 \ xed \ x00 \ x06 \ x00 \ x00 \ x00Biff8 \ x00 \ x0e \ x00 \ x00 \ x00Excel.She et.8 \ x00 \ xf49 \ xb2q \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x ..................... ...

Wenn Sie zum ersten Mal f.read() eingeben, geben Sie '\ xd0 \ xcf \ x11 \ xe0 \ xa1 \ xb1' aus, nicht den gesamten xls-Dokumentinhalt.

Wie kann ich tun?

Antwort

2

Versuchen Öffnen der Datei in Binär-Modus: Man denke

f = open('commend/fixtures/Book2.xls', 'rb') 
0

ich, dass in Unicode gelesen hat wird. Testen Sie die xls-Datei zum Testen einfach in einem anderen Format und sehen Sie, was passiert. Here die Probleme mit Unicode in Python sind verfügbar.