0

我正在使用 NSURLConnection 从服务器下载 mp3 数据,我的代码在这里

- (IBAction)downloadData:(id)sender
 {

   NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
   NSURL *url = [[NSURL alloc] initWithString:@"http://viadj.viastreaming.net/start/psalmsmedia/ondemand/Nin%20snehamethrayo.mp3"];

   [request setURL:url];
   [url release];
   url = nil;

   NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];


 }

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
 {
     responseData = [[NSMutableData alloc] init];
 }

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
 {
   [responseData appendData:data];
 }

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
 {
  [responseData release];
  [connection release];

 }

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
 {

  NSLog(@"Succeeded! Received %d bytes of data",[responseData
                                               length]);
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile"];

  [responseData writeToFile:fileName atomically:YES];

  responseData = nil;
  self->imageConnection = nil;


  }

我对下载的路径有点困惑。当我单击下载按钮时,我显示“成功!收到 1329 字节的数据”,但没有下载任何内容。需要一些帮助。我们将如何指定 iPhone 的本地路径来存储下载的数据?

4

2 回答 2

2
- (IBAction)downloadData:(id)sender
{
  NSURL *url = [[NSURL alloc] initWithString:@"http://viadj.viastreaming.net/start/psalmsmedia/ondemand/Nin%20snehamethrayo.mp3"];
  NSMutableURLRequest   *theRequest_to = [NSMutableURLRequest requestWithURL:url];
  [url release];
  NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:theRequest_to delegate:self];

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response
{
  NSString *filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"snehamethrayo.mp3"]; // here you can set your filename which you can get from url 
  [[NSFileManager defaultManager] createFileAtPath:filepath contents:nil attributes:nil];
  file = [[NSFileHandle fileHandleForUpdatingAtPath:filepath] retain];// Here file is object of NSFileHandle and its declare in .h File
  [file seekToEndOfFile];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
  [file seekToEndOfFile];
  [file writeData:data];
}


- (void)connectionDidFinishLoading:(NSURLConnection*)connection 
{
 [file closeFile];
}
于 2012-05-09T05:48:09.450 回答
0

我认为不需要任何代码更改。只需放置一个 nslog 并查看...

NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile"];
NSLog(@"%@",fileName);

这将列出像这样的文件位置 /Users/me/Library/Application Support/iPhone Simulator/5.0/Applications/(your app)/Documents/myFile. 即下载的文件在您的文件夹中。

注意:不要忘记把文件格式ie

NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile.mp3"];
于 2012-05-09T06:07:07.093 回答