0
votes

I am trying to upload image to server but always getting error from server.

Headers.

Content-Type: multipart/form-data; boundary=---BOUNDARY

Body

-----BOUNDARY

Content-Disposition: form-data; name="file"; filename="pic.jpg"

Content-Type: image/jpeg

Content-Transfer-Encoding: base64

/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0a HBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIy MjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAABAAEDASIA AhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAf/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFAEB AAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/AL+AD//Z -----BOUNDARY

I have using below code

-(NSString *)uploadFileToServer:(NSString *)fileName fileData:(NSData *)file1Data
{
NSString *urlString =url.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---BOUNDARY";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
NSString *header = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",@"file",@"pic.jpg"];
[body appendData:[[NSString stringWithFormat:@"\r\n–%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:header] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:file1Data]];
[body appendData:[[NSString stringWithFormat:@"\r\n–%@–\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] ;
return returnString;
}

Response

"details": {

    "errors": {

      "file": "is required"
    }
  },
  "error": "Invalid files",

  "error_code": 4,

  "exc_class": "FAPIValidationError",

  "rc": false
1
if you know about afnetworking then please go with this to upload fileNitin Gohel
Actually I have a code is swift2.3user6777252
thanks for your response, can you let me know please which part of code is wrong?Amit Kumar
Have you tried these all links: google.co.in/…user6777252

1 Answers

0
votes

Have you tried using alamofire?

Alamofire.upload(multipartFormData: { (multipartFormData) in
// put your data to multipart here                        
}, to: url) { (result) in

    switch result {
    case .success(let upload, _, _):

    upload.responseJSON { response in

        switch response.result {
        case .success(let json):
        break:    
        case .failure(_):
        break:    
        }
    }
    case .failure(_):
    break:
    }
}