ios--django 图片上传

 

IOS客户端请求方法: 

 NSData *data = [NSDatadataWithContentsOfFile:[[NSBundlemainBundle] pathForResource:[NSStringstringWithFormat:@"%d",7] ofType:@"jpg"]];

//        NSLog(@"data is %d",[data length]);

        ASIFormDataRequest *uploadRequest = [ASIFormDataRequestrequestWithURL:[NSURLURLWithString:@"http://115.28.139.99:8000/search/"]];

        //    [uploadRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

        [uploadRequest addRequestHeader:@"Content-Type"value:@"multipart/form-data"];

        [uploadRequest setRequestMethod:@"POST"];

        [uploadRequest setPostValue:@"Django fuck you" forKey:@"q"];

        [uploadRequest setPostValue:@"Django fuck you qqqq"forKey:@"qqq"];

        [uploadRequest addData:data withFileName:@"file1"andContentType:@"image/jpg"forKey:@"file"];

        //    [uploadRequest addData:data forKey:@"aa"];

        uploadRequest.delegate = self;

        [uploadRequest startAsynchronous];

 

 

Django view.py 处理操作:

defhandle_uploaded_file(f):

    f_path = '/root/Django/mysite/images/testtow.jpg'

    with open(f_path ,'wb+') as info:

        print f_path

        for chunk in f.chunks():

            info.write(chunk)

    return f_path

 

def search(request):

    #return HttpResponse('OKOK')

    f = handle_uploaded_file(request.FILES['file'])

    return HttpResponse('123')

你可能感兴趣的:(django)