如何上传图像到服务器与标头在ios
问题描述:
我试图上传图像到服务器与标题希望我的代码是好的,但我不知道它不能上传。我的代码是如何上传图像到服务器与标头在ios
{
NSDictionary *headers = @{ @"content-type":@"multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
@"p-auth-token":self.token};
NSString *urlString = [ NSString stringWithFormat:@"http://ica.com/facilitator/server/v1/media"];
UIImage *image= profile_Image;
NSData *imageData = UIImageJPEGRepresentation(image, 0.1);
double my_time = [[NSDate date] timeIntervalSince1970];
NSString *imageName = [NSString stringWithFormat:@"%d",(int)(my_time)];
NSString *string = [NSString stringWithFormat:@"%@%@%@", @"Content-Disposition: form-data; name=\"file\"; filename=\"", imageName, @".jpg\"\r\n\""];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setAllHTTPHeaderFields:headers];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:string] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *statusDict = [[NSDictionary alloc]init];
statusDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
[SVProgressHUD dismiss];
NSLog(@"Images form server %@", statusDict);
}] resume];
}
答
尝试AFNetworking与下面的代码,
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"upload_url" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:data name:@"upload_file_name" fileName:@"somefilename.jpg" mimeType:@"mime_type_of_file"] // you file to upload
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
dispatch_async(dispatch_get_main_queue(), ^{
//Update the progress view
[progressView setProgress:uploadProgress.fractionCompleted];
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
[uploadTask resume];
我应该在哪里这里举头?它给请求失败:未授权(401)错误。 – Loki
像这样,[formData appendPartWithHeaders:@ {@“Content-Type”:@“application/x-www-form-urlencoded; charset = utf-8”} body:nil]; [formData appendPartWithFileData:数据名称:@“bytes”fileName:@“newimage.jpeg”mimeType:@“image/jpeg”]; –