liveservices / LiveSDK-for-iOS

LiveSDK library for integrating with Live Connect
MIT License
138 stars 84 forks source link

Resume upload and uploading by pieces of data #35

Open iPermanent opened 10 years ago

iPermanent commented 10 years ago

I found that the upload only support NSData and NSInputStream, however when i upload an file larger than 1GB, the app will crash as the memory is limited, I found that someone adjust me to upload by NSFileHandler to post data by pieces, I separated the file to every piece of 1MB, and use the http-header "Content-Range", but it seems not work, it can only upload the first 1MB, and this is my code according to the msdn library, I didn't know if the server support this way: NSData putData = [self uploadSubdataWithOffset:offset length:dataLength]; NSString rangeString = [NSString stringWithFormat:@"bytes */%lu-%lu/%lu",(unsigned long)offset,(unsigned long)offset+dataLength-1,(unsigned long)dataLength]; [_request setValue:rangeString forHTTPHeaderField:@"Content-Range"]; [_request setValue:[NSString stringWithFormat:@"%llu",(unsigned long long)_chunckSize] forHTTPHeaderField:@"Content-Length"]; [_request setValue:[NSString stringWithFormat:@"%llu",[_fileHandler seekToEndOfFile]] forHTTPHeaderField:@"X-Upload-Content-Length"]; [_request setValue:[self fileMimeType:uploadFilePath] forHTTPHeaderField:@"X-Upload-Content-Type"];

[bodyData resetBytesInRange:NSMakeRange(0, [bodyData length])];
[bodyData setLength:0];
NSString * boundary = @"A300x";
[bodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"\r\n", uploadFileName] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", @"application/octet-stream"] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:putData];
[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[_request setHTTPBody:putData];

_connection = [[NSURLConnection alloc] initWithRequest:_request delegate:self];
[_connection start];

As I need the uploading pause and resume, and the large file must support as well,so I had to do it by this, can you give me any help about this? Thanks very much for your patient