ios - 从iphone上传图片到服务器文件夹

标签 ios iphone image upload directory

我在网上发现很少有片段可以将图像从 iphone 上传到服务器文件夹,它显示使用服务器端脚本,例如。在服务器端使用 php

<?php
$uploaddir = 'uploads/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "Uploaded!";
}else{
   echo "Not Uploaded!";
}
/>

是否可以不用上面的代码直接上传图片到服务器文件夹

假设我想上传到 http://111.22.333.44/mysite/pics/ w/o 任何服务器端脚本,如果可以如何做到这一点

最佳答案

/*
  turning the image into a NSData object
  getting the image back out of the UIImageView
  setting the quality to 90
 */
 NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
 // setting up the URL to post to
 NSString *urlString = @"http://iphone.zcentric.com/test-upload.php";

 // setting up the request object now
 NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
 [request setURL:[NSURL URLWithString:urlString]];
 [request setHTTPMethod:@"POST"];

 /*
  add some header info now
  we always need a boundary when we post a file
  also we need to set the content type

  You might want to generate a random boundary.. this is just the same 
  as my output from wireshark on a valid html post
 */
 NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
 NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
 [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

 /*
  now lets create the body of the post
 */
 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=\"ipodfile.jpg\"\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]];
 // setting the body of the post to the reqeust
 [request setHTTPBody:body];

 // now lets make the connection to the web
 NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
 NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

 NSLog(returnString);

此代码将对您有所帮助....

关于ios - 从iphone上传图片到服务器文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1939335/

相关文章:

ios - 以编程方式实现 iMessage

ios - 具有委托(delegate)关系的上一个 View Controller 不执行预期的操作

iphone - iPhone 应用程序中的异常 : Modal transition is already in progress

python - 使用特定像素边距 numpy opencv 从图像中剪切蒙版

ios - 显示一些 View ,仅适用于 iOS 14、SwiftUI

iphone - 创建一个从 iphone 屏幕创建视频并从耳机/音频输入添加音频的应用程序

iphone - AVPlayer 的音频电平表

java - 图形界面java插入图像空间

c++ - 在不使用外部库的情况下在 C++ 中读取灰度 .jpeg 图像?

ios - UITextField 中的自定义图像,如 Venmo 应用程序