0

I am trying to copy an uploaded .xls file to local location in Python. But the copied excel file is having binary characters like,

`èÃÖz­û¬öv¢Ã¿Þ#Ö}®×BˆGŸì“þ¨Kù¯éä:ÅBHoÑ?±M¾«fò¶Ü2›§]¹Õ®JiËXgE*æÆQÁ¯¸&ÿŒ¨#ºÎsè=³`îž5¹‰nЬËΰ±$õºé/š]´cZ̤Bž’8*ûM¸¾,ú£.ú]ÑwÑëü‡þR2*7ª`

What changes can I make in my code to make it working. I am using the following code,

index.html file:

<body>
    <form name='getXlFileForm' action='reader.py' method='post'  enctype='multipart/form-data'>
        <input type='file' name='xlFileFromForm' />
        <input type='submit' value='Upload' />
    </form>
</body>

reader.py file:

cgitb.enable()
requestForm = cgi.FieldStorage()
uploadedFiles = requestForm['xlFileFromForm']

if uploadedFiles.filename:
    tf = tempfile.NamedTemporaryFile(prefix='c:/myOut/', suffix='.xls', delete=False)
    uploadedName = os.path.basename(uploadedFiles.filename)
    open(tf.name,"w").write(uploadedFiles.file.read())
#also tried "wb" mode
Ashwin
  • 993
  • 1
  • 16
  • 41

1 Answers1

0

I am puzzled why the file is turned into binary format during upload

Your file has been a binary file since you created it. You just typically look at it using a software program that interprets the binary data into rows and columns, with text data, formulas, graphics, etc.

If you look at the .xls file in a program that doesn't know how to convert the binary data into a spreadsheet, like a text editor, then the binary data is converted to characters, which generally looks like gibberish.

Here is what a portion of an .xls file looks like that I opened in a text editor:

#0_);\("$"#,##0\)..!......"$"#,##0_);[Red]\("$"#,##0\).."......"$"#,##0.00_);\("$"#,##0.00\)..'...".."$"#,##0.00_);[Red]\("$"#,##0.00\)..7.*.2.._("$"*
#,##0_);_("$"* \(#,##0\);_("$"* "-"_);_(@_)....).).._(* #,##0_);_(* \(#,##0\);_(* "-"_);_(@_)..?.,.:.._("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)..6.+.1.._(* #,##0.00_);_(* \(#,##0.00\);_(* "-"??_);_(@_).

Edit:

You definitely want to write in binary mode: wb. See if you can get something simpler to work:

#!/usr/bin/env python
import cgi
import cgitb
cgitb.enable()

requestForm = cgi.FieldStorage()
uploadedFile = requestForm['xlFileFromForm']

if uploadedFile.file:
    f = open('uploaded.xls', 'wb')  #writes file to the cgi-bin directory
    f.write(uploadedFile.file.read())
    f.close()
    msg = 'wrote file'
else:
    msg = "failed"


html = """Content-type:text/html\r\n\r\n
<html>
<head>
<title>Test CGI</title>
</head>
<body>
<div>Hello: {}</div>
</body>
</html>
""".format(msg)

print html

When I do that, the file size of the new file is exactly the same as the .xls file I uploaded.

html:

 <form name='getXlFileForm' action='cgi-bin/mycgi2.py' method='post'  
    enctype='multipart/form-data'>
    <input type='file' name='xlFileFromForm' />
    <input type='submit' value='Upload' />
  </form>
7stud
  • 46,922
  • 14
  • 101
  • 127
  • I agree with this point. But I uploaded the file and written the file as .xls and trying to open it in same xl application. But the output changed after upload. – Ashwin Jan 25 '15 at 07:29
  • @Ashwin, Then why are you complaining about seeing binary data converted to characters looking like gibberish? What's your real issue? – 7stud Jan 25 '15 at 07:34
  • In my command line I will directly giving my input file in python file. There the file is read properly. But in upload method, my copied file content is getting changed. So my script failing since it can't read the file itself. One more thing is that, my file size itself has reduced from 70K to 51K in upload method – Ashwin Jan 25 '15 at 07:38
  • Hi I tried ur code, still the problem is there. Here is wat i have tried, `cgitb.enable() requestForm = cgi.FieldStorage() uploadedFiles = requestForm['xlFileFromForm'] if uploadedFiles.file: f = open('d:/out.xls','wb') f.write(uploadedFiles.file.read()); f.close();` – Ashwin Jan 25 '15 at 08:37
  • Hi @7stud , i created a dummy xls file with just 7KB and tried uploading. Still the kind of output remains the same 50KB binary file. So I suppose there is either some configuration problem or something – Ashwin Jan 25 '15 at 09:08
  • @Ashwin, What are you using for a server? – 7stud Jan 25 '15 at 09:12
  • I am using Xampp. What server u used? – Ashwin Jan 25 '15 at 09:16
  • @Ashwin, Apache as well. But Apache does not have permissions to write anywhere on my file system. If I try to write to a location outside the apache directory, I get an error in my browser: `type 'exceptions.IOError'>: [Errno 13] Permission denied: '/Users/7stud/uploaded.xls' `. Obviously, your file is not being written--I wonder why you don't get an error in your browser? One of the things I wanted you to try was writing to the apache directory by using the filename: `upload.xls`--but you ignored me and tried to write to `d:/out.xls`. You need to follow directions better. – 7stud Jan 25 '15 at 09:24
  • But i tried uploading a **.html** file. It worked fine for me – Ashwin Jan 25 '15 at 09:29
  • @Ashwin, when somebody tells you to try some code--that means you need to try that EXACT code--you don't know what things the code is meant to probe, so don't go changing the code. Try writing to the filename 'upload.xls' and report back. And, what are you seeing in your browser for the response? – 7stud Jan 25 '15 at 09:30
  • Sorry, now i tried the EXACT code which you provided. The output is unchanged. So what could be the solution for this problem? – Ashwin Jan 25 '15 at 09:35
  • @Ashwin, What do you mean the output is unchanged? Did you go into your Apache directory, then look in the cgi-bin directory for the file upload.xls? Is there a file there? – 7stud Jan 25 '15 at 09:37
  • Yes the file is created. But it has same junk binary dump as I got with my code. – Ashwin Jan 25 '15 at 10:09
  • @Ashwin, The question is whether the size of the uploaded file is the same as the size of the original file. I do not believe that a 7Kb test file turned into a 50Kb file named `upload.js` when uploaded using my code. Are you sure you are looking at the correct files? Are you sure you know how to determine a file's size? – 7stud Jan 25 '15 at 20:39
  • The problem here is no binary file is uploaded correctly. Always the same dump is written into the ouputfile. I a determining the file size in windows only. With my same code text files are uploaded in proper manner. – Ashwin Jan 26 '15 at 04:15
  • @Ashwin, This is what I want to know: 1) Delete ALL uploaded files from your server, and any other directories you tried to write them to. 2) Upload *one* file: your 7Kb test file using my code. 3) What is the size of the uploaded file? Responses like this: *The problem here is no binary file is uploaded correctly*--are meaningless and not helpful. The only thing I want you to post in response to my question is: 1) the size of the upload.xls file on your server, 2) The response you see in your browser. – 7stud Jan 27 '15 at 19:41
  • using both our codes 7Kb **text** file is uploading properly without any file errors or file corruptions. Using the same code if we upload a binary file **(.xls/.doc)** format, no errors are seen from browser side but the uploaded file is seen as unusable binary dump of standard size (around 49.2Kb) always – Ashwin Jan 31 '15 at 03:47