我目前正致力于通过HTTP Post将图像上传到服务器,似乎无法找到构建调用该服务的URL的方法。用户从库或相机中选择一个图像,然后调用执行insert语句的json服务。
该服务期待以下uritemplate:
@"%@/DataTransfer/SetUserProfileImage?EMP_ID=%@&image=%@&imageName=%@"
期望图像数据以某种方式转换为字符串并通过URL发送。
这是我目前的代码:
- (BOOL)setUserProfileImage:(UIImage *)imgUser Name:(NSString *)strName{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSData *dataImage = UIImagePNGRepresentation(imgUser);
NSString* theNSString = [[NSString alloc] initWithData:dataImage encoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/DataTransfer /SetUserProfileImage?EMP_ID=%@&"
"image=%@&imageName=%@",
appDelegate.ServerAddress,
appDelegate.UserId,
theNSString,
strName]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy: NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSURLResponse* response = nil;
NSError* resultError = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&resultError];
NSString *strResult = [[NSString alloc] initWithBytes:[data bytes]
length:[data length]
encoding:NSUTF8StringEncoding];
BOOL imgResponse = [strResult boolValue];
[strResult release];
return imgResponse;
}
我收到错误,说NSURL是“”。似乎无法构建正确的URL。我知道服务本身会再次将此字符串转换为图像。
更新:
- (BOOL)setUserProfileImage:(UIImage *)imgUser Name:(NSString *)strName{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *url = [NSString stringWithFormat:@"%@/DataTransfer/SetUserProfileImage",appDelegate.ServerAddress];
NSData *data = UIImagePNGRepresentation(imgUser);
NSString * boundary = @"tweetPhotoBoundaryParm";
NSMutableData *postData = [NSMutableData dataWithCapacity:[data length] + 1024];
name=\"EMP_ID\"\r\n\r\n%@", @"100-01"];
NSString * boundaryString = [NSString stringWithFormat:@"\r\n--%@\r\n", boundary];
NSString * boundaryStringFinal = [NSString stringWithFormat:@"\r\n--%@--\r\n", boundary];
[postData appendData:[boundaryString dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\";\r\nfilename=\"media.png\"\r\nContent-Type: image/png\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:data];
[postData appendData:[boundaryStringFinal dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest * theRequest=(NSMutableURLRequest*)[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[theRequest setHTTPMethod:@"POST"];
[theRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
//[theRequest addValue:@"www.tweetphoto.com" forHTTPHeaderField:@"Host"];
NSString * dataLength = [NSString stringWithFormat:@"%d", [postData length]];
[theRequest addValue:dataLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:(NSData*)postData];
NSURLConnection * theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
if (theConnection)
{
webData =[[NSMutableData data] retain];
}
else
{
NSLog(@"%@",@"Could not connect to the network");
}
return false;
}
答案 0 :(得分:7)
<强>目标C 强>
-(void)saveImageToServer
{
// COnvert Image to NSData
NSData *dataImage = UIImageJPEGRepresentation([UIImage imageNamed:@"yourImage"], 1.0f);
// set your URL Where to Upload Image
NSString *urlString = @"Your URL HERE";
// set your Image Name
NSString *filename = @"YourImageFileName";
// Create 'POST' MutableRequest with Data and Other Image Attachment.
NSMutableURLRequest* request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:dataImage]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
// Get Response of Your Request
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Response %@",responseString);
}
<强>夫特强>
// COnvert Image to NSData
var dataImage: NSData = UIImageJPEGRepresentation(UIImage(named: "yourImage"), 1.0)
// set your URL Where to Upload Image
var urlString: String = "Your URL HERE"
// set your Image Name
var filename: String = "YourImageFileName"
// Create 'POST' MutableRequest with Data and Other Image Attachment.
var request: NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: urlString)!
request.HTTPMethod = "POST"
var boundary: String = "---------------------------14737809831466499882746641449"
var contentType: String = "multipart/form-data; boundary=\(boundary)"
request.addValue(contentType, forHTTPHeaderField: "Content-Type")
var postbody: NSMutableData = NSMutableData.data()
postbody.appendData("\r\n--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding))
postbody.appendData("Content-Disposition: form-data; name=\"userfile\"; filename=\"\(filename).jpg\"\r\n".dataUsingEncoding(NSUTF8StringEncoding))
postbody.appendData(String.stringWithString("Content-Type: application/octet-stream\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding))
postbody.appendData(NSData.dataWithData(dataImage))
postbody.appendData("\r\n--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding))
request.HTTPBody = postbody
// Get Response of Your Request
var returnData: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
var responseString: String = String(data: returnData, encoding: NSUTF8StringEncoding)
NSLog("Response %@", responseString)
答案 1 :(得分:3)
这种方法有几个问题。
1)将图像的原始字节转换为字符串永远不会有效。
NSData *dataImage = UIImagePNGRepresentation(imgUser);
NSString* theNSString = [[NSString alloc] initWithData:dataImage encoding:NSASCIIStringEncoding];
会失败。您永远无法从dataImage
重建theNSString
。您需要基于64编码dataImage。使用类似this的内容进行base 64编码。
2)不要将图像数据放入URL。您需要将图像数据放在帖子中。
3)不要使用“application / x-www-form-urlencoded”,请使用“multipart / form-data”。
评论后更新
很抱歉,看起来您在理解系统方面还有很多工作要做。
在上面的示例中,您将所有数据添加到URL查询字符串,但未向邮件正文添加任何内容。在该示例中,您将内容类型设置为“application / x-www-form-urlencoded”。
现在你似乎认为POST需要使用JSON。您需要了解应该如何完成。如何附加JSON消息?在你说数据需要在URL中之前,情况仍然如此吗?如果需要将JSON消息附加到POST正文,那么POST的内容类型需要是什么? JSON消息的结构是什么(需要所有键/值字段)?
在任何人可以帮助您之前,您需要找出完全所需的内容。应该有人可以为您提供HTTP消息的准确描述。