Payal Maniyar Payal Maniyar - 4 months ago 51
Objective-C Question

File uploaded using [defaultSession uploadTaskWithRequest:request fromFile:fileURL]; is not uploading file at server side

We want to upload with NSURLSession.
Below is code at Objective C side:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"uploadFileServer"];

NSString *image_name=@"testing.jpg";
image_name=[fileDest stringByAppendingPathComponent:image_name];

NSMutableURLRequest *request =[[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];

NSString *boundary = @"14737809831466499882746641449";

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];;

[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

[request setHTTPMethod:@"PUT"];

NSURLSession *defaultSession= [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:
[NSOperationQueue new]];

NSURL *fileURL =[NSURL fileURLWithPath:image_name];

NSURLSessionUploadTask *uploadTask = [defaultSession uploadTaskWithRequest:request fromFile:fileURL];

[uploadTask resume];


At server side File is not received.
Below is code at server side :

<?php

$body = '';
$fh = @fopen('php://input', 'r');
if ($fh)
{

while (!feof($fh))
{
$s = fread($fh, 1024);
if (is_string($s))
{
$body .= $s;
}
}
fclose($fh);
}

?>


If we upload data of file it works fine but for my requirement I have to upload file in background using method
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL;

Please guide us.

Answer

Finally I have solved my problem. Here is the solution :

 NSString *image_name= fileName;
    image_name=[fileDest stringByAppendingPathComponent:image_name];
    NSMutableData *imageData=[[NSMutableData alloc] init];
    NSMutableURLRequest *request =[[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:fileUploadURL]];
    NSString *boundary = @"14737809831466499882746641449";
    NSString *requestContentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];;
    [request addValue:requestContentType forHTTPHeaderField: @"Content-Type"];
    [request setHTTPMethod:@"PUT"];
    [imageData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [imageData appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"image\"; filename=\"%@\"\r\n",image_name] dataUsingEncoding:NSUTF8StringEncoding]];
    [imageData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [imageData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    NSURL *fileURL =[NSURL fileURLWithPath:image_name];
    NSURLSessionUploadTask *uploadTask = [uploadSession uploadTaskWithRequest:request fromFile:fileURL];

    NSMutableDictionary *UploadInfo = [NSMutableDictionary dictionary];
    [UploadInfo setObject:fileURL forKey:kMZUploadKeyURL];
    [UploadInfo setObject:fileName forKey:kMZUploadKeyFileName];
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:UploadInfo options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    [uploadTask setTaskDescription:jsonString];
    [uploadTask resume];