IOS 后无法在服务器上上传图片
Unable to uploading Images on server in IOS
我无法从我的 IOS 应用程序将图像上传到 php 服务器。
这是我的代码..
NSString *urlString = @"My URl";
NSURL* requestURL = [NSURL URLWithString:urlString];
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
// NSString *boundary = @"---------------------------147378098314876653456641449";
NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy";
NSString* FileParamConstant = @"profileimage";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",BoundaryConstant];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
///---
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
for (NSString *param in dicSubmit)
{
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [dicSubmit objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
NSData *imageData = UIImageJPEGRepresentation(self.imgprofile.image, 0.0f);
if (imageData)
{
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"editprofileimage1.png\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
// set URL
// [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
[request setURL:requestURL];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
尝试使用 AFNetworking
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"your url"]];
NSData *imageData = UIImagePNGRepresentation(self.m_clientPhoto.image);
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setValue:self.nameTextField.text forKey:@"name"];
[dict setValue:self.countryCode1 forKey:@"countrycode"];
[dict setValue:self.phone1 forKey:@"phone"];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"img" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[op start];
我无法从我的 IOS 应用程序将图像上传到 php 服务器。
这是我的代码..
NSString *urlString = @"My URl";
NSURL* requestURL = [NSURL URLWithString:urlString];
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
// NSString *boundary = @"---------------------------147378098314876653456641449";
NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy";
NSString* FileParamConstant = @"profileimage";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",BoundaryConstant];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
///---
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
for (NSString *param in dicSubmit)
{
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [dicSubmit objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
NSData *imageData = UIImageJPEGRepresentation(self.imgprofile.image, 0.0f);
if (imageData)
{
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"editprofileimage1.png\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
// set URL
// [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
[request setURL:requestURL];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
尝试使用 AFNetworking
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"your url"]];
NSData *imageData = UIImagePNGRepresentation(self.m_clientPhoto.image);
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setValue:self.nameTextField.text forKey:@"name"];
[dict setValue:self.countryCode1 forKey:@"countrycode"];
[dict setValue:self.phone1 forKey:@"phone"];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"img" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[op start];