C Ftp Download File Progress Bar

  четверг 16 апреля
      37
C Ftp Download File Progress Bar 4,3/5 3092 votes
-->

Definition

C#/CSharp Upload file to FTP server with progress bar Introduction. (Link 2)Download Net 4.5 source.zip - 18 KB (Link 1)Download Net 3.5 source.zip - 17.4 KB. C#/CSharp Upload file to FTP server with progress. 2010 (1) April (1) 2009 (3). Uploading a file using FtpWebRequest and I want to show a Powershell progress bar. Im uploading a file via FTP (using FtpWebRequest) in Powershell and I want to show a progress bar. Simple progress would be nice but adding remaining time and/or speed would be awesome.

Occurs when an asynchronous download operation successfully transfers some or all of the data.

Examples

The following code example demonstrates setting an event handler for this event.

The following code example shows an implementation of a handler for this event.

Remarks

This event is raised each time an asynchronous download makes progress. This event is raised when downloads are started using any of the following methods.

MethodDescription
DownloadDataAsyncDownloads data from a resource and returns a Byte array, without blocking the calling thread.
DownloadFileAsyncDownloads data from a resource to a local file, without blocking the calling thread.
OpenReadAsyncReturns the data from a resource, without blocking the calling thread.

The DownloadProgressChangedEventHandler is the delegate for this event. The DownloadProgressChangedEventArgs class provides the event handler with event data.

For more information about how to handle events, see Handling and Raising Events.

Note

Ama Sponsors What Program Fbla Practice Test; Ama Sponsors What Program; Dr. Forkner, head of the Commercial Education Department of the Teachers College of Columbia University developed the FBLA concept in 1937. In the fall of 1940, the National Council for Business Education accepted official sponsorship of FBLA; on February 3, 1942, the first high school chapter was chartered in Johnson City, Tennessee. FBLA-PBL sponsors and partners provide many benefits and programs for members and advisers, including educational programs, scholarships, and discount programs. Sponsors generously provide the cash awards and trophies for the top winners of our National Leadership Conference (NLC) competitive events program and other conference activities. Ama sponsors what program fbla practice questions.

A passive FTP file transfer will always show a progress percentage of zero, since the server did not send the file size. To show progress, you can change the FTP connection to active by overriding the <xref:System.Net.WebClient.GetWebRequest%2A> virtual method:

Applies to

In this tutorial, we will download a file by HTTP and display the downloading progress by progress bar. The final screenshot is like this:

Downloading File

To display the downloading progress, we need know file size and currently downloaded bytes size.

So how to get the size of file need download? If you’re familiar with HTTP, it will go easy. When downloading a file, first client will send a HTTP request to server for the file URL, then the server will send back a HTTP response to client. If requested URL is valid, the response body will be the file content.In the response header, there’s a field called “Content-Length”, which is used to indicate size of the response body. Since response body here is the file content, so this “Content-Length” is what we’re looking for.

First we need use urllib2.urlopen() to open an url, this method returns a response object. Chain method info().get_headers() of response object can fetch the response header.So we can get file size like this:

file_size=r.meta().get_headers(['Content-Length'])[0]

And next, we need know how many bytes we have downloaded.
the read(n) method of response object will receive n bytes of data from server. Actually read() can be called with no parameter, then it will receive all data sent by server. But calling read() method like that will let urllib2 module to handle the whole transferring progress. we cannot know how many bytes have been downloaded until downloading is finished.

To get currently downloaded bytes size, we need download fixed size of bytes everytime and update the downloaded bytes size after that block of bytes are downloaded. Then continue downloading until all data is received. You got the idea? so lets implement it.

2
4
6
8
10
12
14
16
18
20
22
importos
u=urllib2.urlopen('http://ck.kolivas.org/apps/cgminer/3.3/cgminer-3.3.0-windows.zip')
file_size=int(meta.getheaders('Content-Length')[0])
# save downloaded file to TEMP directory
f=open(os.path.join(os.environ['TEMP'],'cgminer.zip'),'wb')
downloaded_bytes=0
whileTrue:
ifnotbuffer:
downloaded_bytes+=block_size

Display Progress Bar

Now we need QProgressBar to display our downloading progress. set_value() method of QProgressBar is used to set value of progress bar. The default value range is 0-100. and you can modify them by using setMinimum() and setMaximum() of QProgressBar.
Following code will create a window and put a progress bar in it. a setProgress() method is provided to set the progress bar value, so the downloading thread can set progress bar value by calling this method.

2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
importsys
fromPySide.QtGui import*
classDownloadingWindow(QWidget):
QWidget.__init__(self)
vbox=QVBoxLayout()
label.setAlignment(Qt.AlignCenter)
self.progress_bar.setAlignment(Qt.AlignCenter)
defsetProgress(self,value):
value=100
os._exit(0)
if__name__'__main__':
w=DownloadingWindow()
app.exec_()

Integration

Progress

We will need put downloading part in a separate thread, otherwise it will block UI. Lord of the rings total war.

So here’s the complete source code

2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
importsys
importos
fromPySide.QtCore import*
def__init__(self,url,tmp_file_name,downloading_window):
self.url=url
self.downloading_window=downloading_window
defrun(self):
meta=u.info()
file_size=int(meta.getheaders('Content-Length')[0])
f=open(os.path.join(os.environ['TEMP'],self.tmp_file_name),'wb')
downloaded_bytes=0
whileTrue:
ifnotbuffer:
downloaded_bytes+=block_size
self.downloading_window.setProgress(float(downloaded_bytes)/file_size*100)
f.close()
return
classDownloadingWindow(QWidget):
QWidget.__init__(self)
vbox=QVBoxLayout()
label.setAlignment(Qt.AlignCenter)
self.progress_bar.setAlignment(Qt.AlignCenter)
defsetProgress(self,value):
value=100
os._exit(0)
app=QApplication(sys.argv)
w=DownloadingWindow()
DownloadThread('http://ck.kolivas.org/apps/cgminer/3.3/cgminer-3.3.0-windows.zip','cgminer.zip',w).start()
app.exec_()