首先,我知道这个问题已经被问过一千次了。我再次询问,因为我已经尝试了其他示例中的解决方案,但它们对我不起作用,我不知道为什么。每个人的方法似乎都略有不同。
NSData *imageData = UIImagePNGRepresentation(form.image);
NSURL *url = [NSURL URLWithString:@"myscript.php"];
NSMutableString *postParams = [[NSMutableString alloc] initWithFormat:@"&image=%@", imageData]];
NSData *postData = [postParams dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [[NSString alloc] initWithFormat:@"%d", [postData length]];
NSMutableURLRequest *connectRequest = [[NSMutableURLRequest alloc] init];
[connectRequest setURL:url];
[connectRequest setHTTPMethod:@"POST"];
[connectRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[connectRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//[connectRequest setValue:@"image/png" forHTTPHeaderField:@"Content-Type"];
[connectRequest setHTTPBody:postData];
NSData *receivedData;
NSDictionary *jsonData;
NSURLConnection *connectConnection = [[NSURLConnection alloc] initWithRequest:connectRequest delegate:self];
NSError *error = nil;
if (!connectConnection) {
receivedData = nil;
NSLog(@"The connection failed!");
} else {
NSLog(@"Connected!");
receivedData = [NSURLConnection sendSynchronousRequest:connectRequest returningResponse:NULL error:&error];
}
if (!receivedData) {
NSLog(@"Data fetch failed: %@", [error localizedDescription]);
} else {
NSLog(@"The data is %lu bytes", (unsigned long)[receivedData length]);
NSLog(@"%@", receivedData);
if (NSClassFromString(@"NSJSONSerialization")) {
id object = [NSJSONSerialization JSONObjectWithData:receivedData options:0 error:&error];
if (!object) {
NSLog(@"JSON Serialization failed: %@", [error localizedDescription]);
}
if ([object isKindOfClass:[NSDictionary class]]) {
jsonData = object;
NSLog(@"json data: %@", jsonData);
}
}
}
目前我正在 postParams 中传递 NSData 并使用此 php 脚本:
if (isset($_POST['image']) && !empty($_POST['image'])) {
if (file_put_contents('images/test.png', $_POST['image'])) {
echo '{"saved":"YES"}'; die();
} else {
echo '{"saved":"NO"}'; die();
}
}
这是将数据保存到文件中,但我无法打开它,因为它已损坏或类似的情况。这几乎是最后的努力,我真的没想到它会以这种方式工作,但到目前为止,它已经非常接近正确的结果了。
我尝试过使用各种内容标题/边界/$_FILES/enctype 内容类型方法,但我什至无法将其正确发送到脚本。
- 顺便说一句,我不只是发送图像数据,我还在 postParams 中发布其他值,这些值只是字符串、整数等。
有谁对此有任何建议或知道任何好的来源吗?
感谢您提供的任何帮助。
遵循以下答案中给出的建议后的当前状态(还有来自程序其他部分的进一步信息):
图像的初始捕获:
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.view endEditing:YES];
__unused form *form = self.form;
form.signature = self.signatureDrawView.bp;
UIGraphicsBeginImageContext(self.signatureDrawView.bounds.size);
[self.signatureDrawView.layer renderInContext:UIGraphicsGetCurrentContext()];
campaignForm.signatureImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
其中signatureDrawView是UIView,form.signature是UIBezierpath。
then...
NSData *sigImage = UIImagePNGRepresentation(campaignForm.signatureImage);
它被传递给以下函数:
- (void)uploadImage:(NSData *)imageData
{
NSMutableURLRequest *request;
NSString *urlString = @"https://.../upload.php";
NSString *filename = @"uploadTest";
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=\"file\"; filename=\"%@.png\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:imageData]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString;
returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
}
upload.php 看起来像:
if ($_FILES["file"]["error"] > 0) {
echo '{"file":"'.$_FILES['file']['error'].'"}';
die();
} else {
$size = $_FILES["file"]["size"] / 1024;
$upload_array = array(
"Upload"=>$_FILES["file"]["name"],
"Type"=>$_FILES["file"]["type"],
"Size"=>$size,
"Stored in"=>$_FILES["file"]["tmp_name"]
);
//echo json_encode($upload_array);
if (move_uploaded_file($_FILES["file"]["tmp_name"], "signatures/" . $_FILES["file"]["name"])) {
echo '{"success":"YES"}';
die();
} else {
echo '{"success":"NO"}';
die();
}
die();
}
这给了我 {success:NO} 输出,并且 $upload_array 转储显示空值。