objective-c - 从 iOS 设备上传图片到服务器

标签 objective-c ios cocoa-touch

我正在尝试将图像上传到远程服务器。如果我在手机上从谷歌下载图片并上传它们,例如 http://www.mangauk.com/gallery/albums/album-11/lg/scooby.jpeg它上传就好了。但是,如果我尝试上传我用相机拍摄的图像,它不起作用。服务器只是挂起。

(IBAction)uploadImage {
    /*
     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:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="userfile"; filename="ipodfile.jpg"rn"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",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);
}

和 php 脚本

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

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "http://iphone.zcentric.com/uploads/{$file}";
}

最佳答案

问题一定出在您的 php 脚本中,因为必须将文件发送给它。

您应该在 php 中定义文件名,而不是相信原始文件名。

否则,有人可能会上传恶意的 .php 文件,而您的脚本会很乐意上传该文件并将其命名为 .php,他们可能会接管您的服务器。

你应该这样做:

$uploaddir = './uploads/';
$file = $uploaddir . 'filename_chosen_by_you.jpg';

这也可能会解决 jpeg/jpg 问题,因为您正在命名。

另一件事——你也在做一个同步请求——这意味着你的应用程序将在执行请求时等待并卡住(如果它是一个大文件,这可能是几秒钟),看看做一个异步请求.这意味着它将在后台上传图片,然后在完成后调用一个函数,这样您就可以告诉用户一切都已上传好。

关于objective-c - 从 iOS 设备上传图片到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12004054/

相关文章:

ios - 使用 cocoapods 安装 SSZipArchive

iphone - 如何以编程方式将 UIToolbar 添加到 UITableViewController?

ios - 单击按钮时隐藏 TabBar 并显示 NavigationController 工具栏

ios - 基于一对多关系的谓词

ios - 解析Json获取一个NSArray中的所有内容

ios - 是否可以将应用程序添加到 native 共享对话框?

ios - Quickblox iOS - 请求对象,按 'Created At' 排序

objective-c - UIPrintInteractionController presentFromRect 问题

iphone - 有没有办法永久保存游戏数据?

ios - 在模拟器上旋转 iPhone 时出现 UICollectionViewCell 大小调整问题