0

I wanted to upload an image from my ios app to django server. I wrote my server according to this answer. It has this code to receive a POST, I can successfully upload files from the browser.

if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = BasicImage(docfile = request.FILES['docfile'])
            newdoc.save()

And I want to use AFNetwork to upload from the ios side like this:

- (IBAction)BtnClick:(id)sender {
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSURL *URL = [NSURL URLWithString:@"http://my.ip.address/myapp/list/"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURL *filePath = [NSURL fileURLWithPath:@"file:1.jpeg"];
    NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {
            NSLog(@"Success: %@ %@", response, responseObject);
        }
    }];
    [uploadTask resume];
}

According to the AFNetwork documentation. After running this app, I got this message:

uploadTest[4217:225627] Success: <NSHTTPURLResponse: 0x7fcbd8c2ad60> { URL: http://10.211.55.3:8000/myapp/list/ } { status code: 200, headers {
    "Content-Type" = "text/html; charset=utf-8";
    Date = "Wed, 23 Dec 2015 12:53:48 GMT";
    Server = "WSGIServer/0.1 Python/2.7.6";
    "Set-Cookie" = "csrftoken=P7PmjtjKPcWAHptFMhCqVRirwkE669bh; expires=Wed, 21-Dec-2016 12:53:48 GMT; Max-Age=31449600; Path=/";
    Vary = Cookie;
    "X-Frame-Options" = SAMEORIGIN;
} }

But there is no 1.jpeg in the server end. I am new to IOS and Django, does anyone how to do this?

Community
  • 1
  • 1
Demonedge
  • 1,363
  • 4
  • 18
  • 33

1 Answers1

0

Your URL response is kind of text/html add this line

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

  • It gives me this error when adding above line: Property 'acceptableContentTypes' not found on object of type 'id' – Demonedge Dec 24 '15 at 11:56