开发者

Problem in NSData in downloading

i have one file with name My book.doc .but at time of downloading from url ,NSData showing 0 bytes. Here is my code

   NSString *imagelist = @"http://SomeUrl/My book.doc";
NSData *yourdoceFileData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imagelist]];
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString 开发者_如何学C*yourFilePath = [documentsDirectory stringByAppendingPathComponent:reString];
[yourdocFileData writeToFile:yourFilePath atomically:YES];

if i remove whitespace in filename and also in string ,then it works properly. tell me 1 thing is it possible for download with whitespace between filename.


You need to encode the URL. Spaces are not valid in an URL. A space character is '%20", so this should work:

NSString *imagelist = @"http://SomeUrl/My%20book.doc";

I use the following function to do the encoding:

- (NSString *)urlEncodeValue:(NSString *)str
{
    NSString *result = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)str, NULL, CFSTR("?=&+"), kCFStringEncodingUTF8);
    return [result autorelease];
}


URL's should not contain whitespace. Replace with space with "%20":

NSString *imagelist = @"http://SomeUrl/My%20book.doc";


Replace the space with a "%20", or use the more general stringByAddingPercentEscapesUsingEncoding method of NSString.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜