iphone向Web服务器发送图片
程序员文章站
2022-05-09 11:21:55
...
//把图片转换为NSData UIImage *image = [UIImage imageNamed:@"vim_go.png"]; NSData *imageData = UIImagePNGRepresentation(image); // post url NSString *urlString = @"http://10.28.4.162/test-upload.php"; // setting up the request object now NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; // NSString *boundary = [NSString stringWithString:@"---------------------------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:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"vim_go.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithString:@"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]; NSLog(@"%@",body); NSLog(@"%@",request);
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; result_btn.text = returnString;
/Applications/XAMPP/htdocs
imac:htdocs aitracy$ cat test-upload.php
<?php
$uploaddir = './upload/';
echo "recive a image";
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "/uploads/{$file}";
}
?>
上一篇: 心脏出血漏洞修复记录
下一篇: iphone向Web服务器发送图片
推荐阅读
-
静态文件服务器A和web应用服务器B分开,怎么样在B服务器上传的图片,上传到静态文件服务器应用是PHP写的?
-
Android使用httpPost向服务器发送请求的方法
-
Android向node.js编写的服务器发送数据并接收请求
-
Android使用httpPost向服务器发送请求的方法
-
Android编程向服务器发送请求时出现中文乱码问题的解决方法
-
Android向node.js编写的服务器发送数据并接收请求
-
java实现客户端向服务器发送文件
-
Android编程向服务器发送请求时出现中文乱码问题的解决方法
-
php输入流php://input使用示例(php发送图片流到服务器)
-
php输入流php://input使用示例(php发送图片流到服务器)