状态码:413 - iOS 10上未上传大于1 MB的文件

时间:2017-03-15 09:35:47

标签: ios iphone nginx file-upload ios10

我已使用nginxNSURLSession上传文件的代码实施到服务器进行速度测试。这在iOS 8上正常运行,但在iOS 10上,它正在提供状态代码413(请求实体太大)。

文件大小约为30MB,如果文件大小小于1MB它正常工作。这是我的代码

NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy";

NSString* FileParamConstant = @"file";
NSURL* requestURL = [NSURL URLWithString:kPathForUploadCheck];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:600];
[request setHTTPMethod:@"POST"];

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];
NSString *path = [[NSBundle mainBundle] pathForResource:@“myfile” ofType:@"psd"];    
NSData   *imageData22      = [NSData dataWithContentsOfFile:path];

NSString *base64String = [imageData22 base64EncodedStringWithOptions:0];
NSData *imageData = [base64String dataUsingEncoding:NSUTF8StringEncoding];

if (imageData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setURL:requestURL];

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *backgroundSeesion = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:self delegateQueue: [NSOperationQueue mainQueue]];
NSURL* tempUrl = [NSURL fileURLWithPath:path];
uploadTask = [backgroundSeesion uploadTaskWithRequest:request fromFile:tempUrl];
uploadTask = [backgroundSeesion uploadTaskWithRequest:request fromData:imageData];

[uploadTask resume];

0 个答案:

没有答案