iOS Question Huge Download with Progress

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

use NSURLConnection.

Here is an little example:

I want to load this 4k image into an ImageView:
B4X:
Sub Download_Complete
Log("File was successfully downloaded")

Dim BM As Bitmap
BM.Initialize(File.DirDocuments,"Mountains.jpg")
ImageView1.Bitmap = BM
End Sub

Sub Download_Error(ErrorMessage As String)
Log(ErrorMessage)
End Sub

Sub Button1_Click
Dim NativeMe As NativeObject = Me
NativeMe.RunMethod("Download:::::",Array("http://data.hdwallpapers.im/nature_mountain_4k.jpg",ProgressView1,Label1,File.DirDocuments,"Mountains.jpg"))
End Sub


#If OBJC

UIProgressView * ProgressBar;

NSURLResponse *DownloadedResponse;
NSMutableData *DownloadedData;
NSString *FilePath;
UILabel *ProgressLabel;

-(void)Download: (NSString *)FileUrl :(UIProgressView *)ProgressBarToSet :(UILabel *)LabelToSet :(NSString *)Directory :(NSString *)FileName
{
   ProgressBar = ProgressBarToSet;
   ProgressBar.progress = 0;

   ProgressLabel = LabelToSet;

   FilePath = [Directory stringByAppendingString: FileName];

   DownloadedData = [[NSMutableData alloc] init];
   NSURL *url = [NSURL URLWithString:FileUrl];
   NSURLRequest *request = [NSURLRequest requestWithURL:url];
   NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
   [DownloadedData appendData:data];
   ProgressBar.progress = ((100.0/DownloadedResponse.expectedContentLength)*DownloadedData.length)/100;

   NSString * LabelText = [[NSString stringWithFormat:@"%0.f", (ProgressBar.progress * 100)] stringByAppendingString:@"%"];
   ProgressLabel.text = LabelText;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   [DownloadedData writeToFile:FilePath atomically:YES];
   [self.bi raiseEvent:nil event:@"download_complete" params:@[]];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
   [self.bi raiseEvent:nil event:@"download_error:" params:@[error.localizedDescription]];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  DownloadedResponse = response;
}

#End If

based on: http://www.devfright.com/ios-data-download-progress-bar-tutorial/
 
Last edited:
Upvote 0

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
JanPro,

This code work like a charm!


THANK YOUUUUUUUUUUUUUUUUUUUUUUU!!!


look:

youdown.jpg
 
Upvote 0

chjk

Member
Licensed User
Longtime User
Hi,

use NSURLConnection.

Here is an little example:

I want to load this 4k image into an ImageView:
B4X:
Sub Download_Complete
Log("File was successfully downloaded")

Dim BM As Bitmap
BM.Initialize(File.DirDocuments,"Mountains.jpg")
ImageView1.Bitmap = BM
End Sub

Sub Download_Error(ErrorMessage As String)
Log(ErrorMessage)
End Sub

Sub Button1_Click
Dim NativeMe As NativeObject = Me
NativeMe.RunMethod("Download:::::",Array("http://data.hdwallpapers.im/nature_mountain_4k.jpg",ProgressView1,Label1,File.DirDocuments,"Mountains.jpg"))
End Sub


#If OBJC

UIProgressView * ProgressBar;

NSURLResponse *DownloadedResponse;
NSMutableData *DownloadedData;
NSString *FilePath;
UILabel *ProgressLabel;

-(void)Download: (NSString *)FileUrl :(UIProgressView *)ProgressBarToSet :(UILabel *)LabelToSet :(NSString *)Directory :(NSString *)FileName
{
   ProgressBar = ProgressBarToSet;
   ProgressBar.progress = 0;

   ProgressLabel = LabelToSet;

   FilePath = [Directory stringByAppendingString: FileName];

   DownloadedData = [[NSMutableData alloc] init];
   NSURL *url = [NSURL URLWithString:FileUrl];
   NSURLRequest *request = [NSURLRequest requestWithURL:url];
   NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
   [DownloadedData appendData:data];
   ProgressBar.progress = ((100.0/DownloadedResponse.expectedContentLength)*DownloadedData.length)/100;

   NSString * LabelText = [[NSString stringWithFormat:@"%0.f", (ProgressBar.progress * 100)] stringByAppendingString:@"%"];
   ProgressLabel.text = LabelText;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   [DownloadedData writeToFile:FilePath atomically:YES];
   [self.bi raiseEvent:nil event:@"download_complete" params:@[]];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
   [self.bi raiseEvent:nil event:@"download_error:" params:@[error.localizedDescription]];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  DownloadedResponse = response;
}

#End If

based on: http://www.devfright.com/ios-data-download-progress-bar-tutorial/
It's very good! Do you know ftp how to download or upload file show process?
 
Upvote 0

cloner7801

Active Member
Licensed User
Longtime User
Hi,

use NSURLConnection.

Here is an little example:

I want to load this 4k image into an ImageView:
B4X:
Sub Download_Complete
Log("File was successfully downloaded")

Dim BM As Bitmap
BM.Initialize(File.DirDocuments,"Mountains.jpg")
ImageView1.Bitmap = BM
End Sub

Sub Download_Error(ErrorMessage As String)
Log(ErrorMessage)
End Sub

Sub Button1_Click
Dim NativeMe As NativeObject = Me
NativeMe.RunMethod("Download:::::",Array("http://data.hdwallpapers.im/nature_mountain_4k.jpg",ProgressView1,Label1,File.DirDocuments,"Mountains.jpg"))
End Sub


#If OBJC

UIProgressView * ProgressBar;

NSURLResponse *DownloadedResponse;
NSMutableData *DownloadedData;
NSString *FilePath;
UILabel *ProgressLabel;

-(void)Download: (NSString *)FileUrl :(UIProgressView *)ProgressBarToSet :(UILabel *)LabelToSet :(NSString *)Directory :(NSString *)FileName
{
   ProgressBar = ProgressBarToSet;
   ProgressBar.progress = 0;

   ProgressLabel = LabelToSet;

   FilePath = [Directory stringByAppendingString: FileName];

   DownloadedData = [[NSMutableData alloc] init];
   NSURL *url = [NSURL URLWithString:FileUrl];
   NSURLRequest *request = [NSURLRequest requestWithURL:url];
   NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
   [DownloadedData appendData:data];
   ProgressBar.progress = ((100.0/DownloadedResponse.expectedContentLength)*DownloadedData.length)/100;

   NSString * LabelText = [[NSString stringWithFormat:@"%0.f", (ProgressBar.progress * 100)] stringByAppendingString:@"%"];
   ProgressLabel.text = LabelText;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   [DownloadedData writeToFile:FilePath atomically:YES];
   [self.bi raiseEvent:nil event:@"download_complete" params:@[]];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
   [self.bi raiseEvent:nil event:@"download_error:" params:@[error.localizedDescription]];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  DownloadedResponse = response;
}

#End If

based on: http://www.devfright.com/ios-data-download-progress-bar-tutorial/
How can we show connection speed ?
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Is it possible to use this method with parameters linke Download2?

B4X:
job.Download2("http://www.example.com", _
    Array As String("key1", "value1", "key2", "value2"))

I have to send the access data to the server.

Thanks for help...
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
JanPro,

This code work like a charm!


THANK YOUUUUUUUUUUUUUUUUUUUUUUU!!!


look:

youdown.jpg

Can u share the code how to download a video from youtube?

Is it possible to rip the audio from it to mp3?

Thsnx
 
Upvote 0

Sasuke Sama

Active Member
Licensed User
I Know this old but .. is it posible to download more than one file at the same time with this?
also how to cancel a download?
 
Upvote 0

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
BUt the code is already here:


The sample in the first post is:
B4X:
Dim NativeMe As NativeObject = Me
NativeMe.RunMethod("Download:::::",Array("http://data.hdwallpapers.im/nature_mountain_4k.jpg",ProgressView1,Label1,File.DirDocuments,"Mountains.jpg"))

is just to change for get the .mp4 files
B4X:
NativeMe.RunMethod("Download:::::",Array(strURL,prvDown,lblProgress,File.DirTemp, "videotmp.mp4"))

and to get the mp4 URLS from youtube, you create the libraryiYoutubeParser
 

Attachments

  • iYoutubeParser.zip
    88.1 KB · Views: 114
Upvote 0

Sasuke Sama

Active Member
Licensed User
BUt the code is already here:


The sample in the first post is:
B4X:
Dim NativeMe As NativeObject = Me
NativeMe.RunMethod("Download:::::",Array("http://data.hdwallpapers.im/nature_mountain_4k.jpg",ProgressView1,Label1,File.DirDocuments,"Mountains.jpg"))

is just to change for get the .mp4 files
B4X:
NativeMe.RunMethod("Download:::::",Array(strURL,prvDown,lblProgress,File.DirTemp, "videotmp.mp4"))

and to get the mp4 URLS from youtube, you create the libraryiYoutubeParser
I dont want the youtube downloader
I have my own video files that i want to download
Using the code above i can start to download a file but if i fire the code again with different url it will cancel the old one and start a new one with the new url
What i want is to download more than one file at the same time each with its own progress using a custom list
I also want to know how to pause or stop and resume a download if possible
Thank you for replying i see you're an active IOS member here in the fourm :D
 
Upvote 0
Top