图片未上传到php服务器ios 10 [关闭]
Posted
技术标签:
【中文标题】图片未上传到php服务器ios 10 [关闭]【英文标题】:Image not upload to php server ios 10 [closed] 【发布时间】:2016-12-29 08:38:58 【问题描述】:您没有选择要上传的文件。
这是代码
- (IBAction)changeProfilePic:(id)sender
[[sharedClass sharedInstance]showprogressfor:@"Please wait"];
NSString *urlString = [ NSString stringWithFormat:@"http://domain.com/as/web_services/change_user_image?user_id=%@",self.useridString];
UIImage *image= profile_Image;
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
request.timeoutInterval = 10.0f;
request.HTTPMethod = @"POST";
NSString *boundary = @"011000010111000001101001";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary, nil];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"projectId\"\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSData *stringData = [@"Content-Disposition: form-data;\
name=\"project\";\
fileName=\"photo.jpeg\"\r\n"
dataUsingEncoding:NSUTF8StringEncoding];
[body appendData:stringData];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSDictionary *statusDict = [[NSDictionary alloc]init];
statusDict = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];
NSString *message = [statusDict valueForKey:@"msg"];
[SVProgressHUD dismiss];
NSLog(@" mesaage is %@", message);
【问题讨论】:
将链接中的 http 更改为 https 我的后端团队只使用 http 提供服务,我该如何改变它。 您检查过您的网址吗?开头有一个空格,您应该检查用户ID(如果它是一个字符串)没有任何特殊字符。 @Luca D'Alberti 我也删除了那个空格,我的用户 ID 也不包含任何特殊字符。它给出消息您没有选择要上传的文件。
【参考方案1】:试试这个 -
NSString *urlString = [ NSString stringWithFormat:@"http://domine.com/as/web_services/change_user_image?user_id=%@",self.useridString];
NSData *imageData = UIImageJPEGRepresentation(theImage, 0.5);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
request.timeoutInterval = 10.0f;
request.HTTPMethod = @"POST";
NSString *boundary = @"011000010111000001101001";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary, nil];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"projectId\"\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSData *stringData = [@"Content-Disposition: form-data;\
name=\"project\";\
fileName=\"photo.jpeg\"\r\n"
dataUsingEncoding:NSUTF8StringEncoding];
[body appendData:stringData];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding
【讨论】:
NSURLConnection 在 ios 10 中已弃用您没有选择要上传的文件。
它在我的调试中给出了消息。 theImage 是我的文件名 - NSData *imageData = UIImageJPEGRepresentation(theImage, 0.5); 您可以使用您的个人资料图片 即使我得到那个错误,我也改变了它。【参考方案2】:去掉url中的空格
复制以下代码以解决您的问题
NSString *urlString = [NSString stringWithFormat:@"http://domine.com/as/web_services/change_user_image?user_id=%@",self.useridString];
【讨论】:
我删除了那个空间,但它也不起作用。它给了我这样的http请求以上是关于图片未上传到php服务器ios 10 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章