【问题标题】:Calculating File size before download下载前计算文件大小
【发布时间】:2023-04-08 14:10:01
【问题描述】:

我想要做的解释如下。

我有一个 MP3 文件的网址。 (例如Sound File

当用户启动应用程序时,应该开始下载,为此我实现了以下方法:

-(void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url=[NSURL URLWithString:@"http://xyz.pqr.com/abc.mp3"];
    NSURLRequest *req=[NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:120];

    NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];

    if(con){
        myWebData=[[NSMutableData data] retain];
        } else {
        //          [MainHandler performSelector:@selector(targetSelector:) withObject:nil];
      } 
     }


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    NSLog(@"%@",@"connection established");
    [myWebData setLength: 0];

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"%@",@"connection receiving data");
    [myWebData appendData:data]; 
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"%@",@"connection failed");
    [connection release];
    //  [AlertViewHandler showAlertWithErrorMessage:@"Sorry, there is no network connection. Please check your network and try again."];
//  [self parserDidEndDocument:nil];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];
}

以上方法非常适合下载。但我无法获得要下载的确切大小。我想知道要下载的文件的大小。

【问题讨论】:

    标签: iphone xcode download nsurlconnection


    【解决方案1】:

    只需在传递给以下委托方法的NSURLResponse 对象上使用 expectedContentSize:

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    
    }
    -(NSURLRequest *)connection:(NSURLConnection *)connection
            willSendRequest:(NSURLRequest *)request
           redirectResponse:(NSURLResponse *)response {
    }
    

    像这样:

    long long size = [response expectedContentLength];
    

    【讨论】:

      【解决方案2】:

      是的! ASIHTTP 几乎完成了。

      -(IBAction)btnTapped:(id)sender {
          NSURL *url = [NSURL URLWithString:@"http://sound20.mp3pk.com/indian/omkara/omkara1%28songs.pk%29.mp3"];
          ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
          [request setDownloadDestinationPath:[self pathForLogInCCID]];
          [myProgressIndicator setProgress:0];
      
          [request setDownloadProgressDelegate:myProgressIndicator];
          [request startSynchronous];
      [myProgressIndicator doubleValue]);
      
          [request setDelegate:self];
          [request startAsynchronous];
      }
      
      
      - (void)requestFinished:(ASIHTTPRequest *)request
      {
          // Use when fetching text data
      //  NSString *responseString = [request responseString];
      
          // Use when fetching binary data
      //  NSData *responseData = [request responseData];
      }
      
      - (void)requestFailed:(ASIHTTPRequest *)request
      {
      //  NSError *error = [request error];
      }
      
      -(NSString*)pathForLogInCCID{
          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString *documentsDirectory = [paths objectAtIndex:0];
          NSString *DataPath = [documentsDirectory stringByAppendingPathComponent:@"mySong.png"];
          return DataPath;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-23
        • 1970-01-01
        • 2013-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-20
        相关资源
        最近更新 更多