473,230 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

uploading files to file system/zipping/downloading problems

I am currently uploading a file from a users computer to the file
system on my server using python, just reading the file and writing the
binaries.

total_data=' '
while True:
data = upload_file.file.read(8192)
if not data:
break
total_data += data
f = open(target_file_name, 'wb')
f.write(total_data)
f.close

However when i download the file from the server it is not intact and
it cannot be opened. It is happening with every type of file.

It seemed to be working before and now it is..maybe I goofed up and
deleted something.
However I can't seem to find it.

any ideas??

Aug 20 '06 #1
3 1615
On Sun, 20 Aug 2006 13:49:22 -0700, OriginalBrownster wrote:
I am currently uploading a file from a users computer to the file
system on my server using python, just reading the file and writing the
binaries.

total_data=' '
while True:
data = upload_file.file.read(8192)
if not data:
break
total_data += data
f = open(target_file_name, 'wb')
f.write(total_data)
f.close

However when i download the file from the server it is not intact and
it cannot be opened. It is happening with every type of file.

It seemed to be working before and now it is..maybe I goofed up and
deleted something.
However I can't seem to find it.

any ideas??
You are re-creating, and overwriting, your file every time you go into
that while loop.

Aug 20 '06 #2
Assuming your upload_file.file.read() function works, the overwriting
should be the only issue. Just want to point out that total_data +=
data is not advisable for this example (speed/efficiency issue,
although i'm sure it could probably be even faster than what I replace
it with if u decided to use arrays, but it's probably not worth it XD),
and I think open() was replaced by file(), although they're probably
interchangable.

new code
-----------------
total_data = ' '
temp_data = []
while True:
data = upload_file.file.read(8192)
if not data:
break
temp_data.append(data)
total_data = ' '.join(temp_data)
somefile = file(target_file_name, 'wb')
somefile.write(total_data)
f.close()
-------------------

OriginalBrownster wrote:
I am currently uploading a file from a users computer to the file
system on my server using python, just reading the file and writing the
binaries.

total_data=' '
while True:
data = upload_file.file.read(8192)
if not data:
break
total_data += data
f = open(target_file_name, 'wb')
f.write(total_data)
f.close

However when i download the file from the server it is not intact and
it cannot be opened. It is happening with every type of file.

It seemed to be working before and now it is..maybe I goofed up and
deleted something.
However I can't seem to find it.

any ideas??
Aug 21 '06 #3
OriginalBrownster wrote:
I am currently uploading a file from a users computer to the file
system on my server using python, just reading the file and writing the
binaries.

total_data=' '
while True:
data = upload_file.file.read(8192)
if not data:
break
total_data += data
f = open(target_file_name, 'wb')
f.write(total_data)
f.close

However when i download the file from the server it is not intact and
it cannot be opened. It is happening with every type of file.

It seemed to be working before and now it is..maybe I goofed up and
deleted something.
However I can't seem to find it.

any ideas??
Two problems:

First, If you start total_data with a single space (as it shows in your
posted code). Then the output file has a space prepended to the
file's contents and that is NOT what you probably wanted.

Secondly, You are overwriting the files contents every time through
the loop. Your open, would need to be outside the loop (above the
while) and your close should be outside the loop also. Something more
like:

total_data=''
f = open(target_file_name, 'wb')

while True:
data = upload_file.file.read(8192)
if not data:
break
total_data += data
f.write(total_data)

f.close

You should take a look at shutil module. It is copyfile method
that makes your code must simpler (and faster I'm sure).

do

import shutl
help(shutil.copyfile)

import shutil
shutil.copyfile(uload_file_name, target_file_name)
-Larry Bates
Aug 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: tony melnyk | last post by:
I'm currently constructing a site one page of which I wish to allow the user to select from a number of different file type downloads. The file types will be .docs .jpgs mp3. I know how to...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
0
by: TJ | last post by:
Hi, I've written code web-based uploading and downloading. Here is some code for it. For saving file into MS-SQL database, SaveFileIntoDB(HttpPostedFile file) { int fileLength =...
4
by: Himanshu | last post by:
hi, Can anybody tell me that thru asp.net using c#, how can we upload and download physical files in any table of SQL Server Database. the uploading part is running successfully but the...
5
by: Nathan Sokalski | last post by:
I am trying to write code to allow my users to upload a file. The code I am using is as follows: Dim upfilename As String = "" If fileDetails.Value <> "" AndAlso...
2
by: Andy | last post by:
My users need to store and retrieve various files (pdfs, docs, jpegs etc). I am developing an asp.net 2.0 web application, with SQL server backend. I've been looking at my best options and am quite...
0
by: mivey4 | last post by:
I have been experimenting with sending and receiving files using the webClient class provided in .NET. (C#) I don't seem to have any problems downloading files from my test web server using the...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
0
by: najmi | last post by:
hai. i have one problem that is to upload file..it working perfectly in my computer but fail when deploy at server..the system is to browse the file,then system will zip it before upload it to the...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.