473,796 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.fil e.read(8192)
if not data:
break
total_data += data
f = open(target_fil e_name, 'wb')
f.write(total_d ata)
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 1634
On Sun, 20 Aug 2006 13:49:22 -0700, OriginalBrownst er 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.fil e.read(8192)
if not data:
break
total_data += data
f = open(target_fil e_name, 'wb')
f.write(total_d ata)
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.fil e.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.fil e.read(8192)
if not data:
break
temp_data.appen d(data)
total_data = ' '.join(temp_dat a)
somefile = file(target_fil e_name, 'wb')
somefile.write( total_data)
f.close()
-------------------

OriginalBrownst er 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.fil e.read(8192)
if not data:
break
total_data += data
f = open(target_fil e_name, 'wb')
f.write(total_d ata)
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
OriginalBrownst er 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.fil e.read(8192)
if not data:
break
total_data += data
f = open(target_fil e_name, 'wb')
f.write(total_d ata)
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_fil e_name, 'wb')

while True:
data = upload_file.fil e.read(8192)
if not data:
break
total_data += data
f.write(total_d ata)

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.cop yfile)

import shutil
shutil.copyfile (uload_file_nam e, target_file_nam e)
-Larry Bates
Aug 21 '06 #4

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

Similar topics

15
3097
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 transfer the files to the server but what code do I need to use to allow user to select and download specific files? Obviously they can right click on an image and download it that way but what about zipped files? Regards Tony
13
4324
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 that this is suppossed to end up as a component for others to use, and therefore I do NOT have access to their global.cs::Session_End() how do I cleanup files that were uploaded -- but obviously left stranded when the users aborted/gave up writting...
0
1526
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 = file.ContentLength; byte fileContent = new byte; int lastPos = file.FileName.LastIndexOf('\\');
4
2155
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 problem arises in the retriving part of the code. i am not getting that how will i able to download the file which is there in the SQL Server database in the field type "image".
5
4687
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 fileDetails.PostedFile.ContentLength > 0 Then Dim dir As String() = fileDetails.PostedFile.FileName.Split("\".ToCharArray())
2
1841
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 new to web development. I am thinking about this process: 1) Upload the doc 2) zip it 3) Store in the db / on the server Can anyone give me some advice and pros and cons on the following:
0
1225
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 class but sending (uploading) files to the server doesn't seem to be working and I don't receive any errors to suggest there is a problem. WebClient wc = new WebClient(); byte results; string uri =...
221
367758
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 needs to store entire files, the preferred method is to save the file onto the server’s file-system, and store the physical location of the file in your database. This is generally considered to be the easiest and fastest way to store files. ...
0
1923
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 server..when a client browse file,the server will generate error that is file not found.here is my code: try { //this is a code to read and zipfile String dir = request.getParameter("dirs"); ...
0
9683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10457
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10231
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10013
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9054
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.