Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Resume upload and uploading by pieces of data #35

Open
iPermanent opened this issue Jun 27, 2014 · 0 comments
Open

Resume upload and uploading by pieces of data #35

iPermanent opened this issue Jun 27, 2014 · 0 comments
Assignees

Comments

@iPermanent
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants