【问题标题】:Xcode - upload image from Iphone Simulator to local folder using PHPXcode - 使用 PHP 将图像从 Iphone Simulator 上传到本地文件夹
【发布时间】:2015-01-06 02:48:39
【问题描述】:

我正在关注此人的tutorial,了解如何将图像从 Xcode 上传到我计算机上的本地文件夹。

当我编译并运行代码时,一切都很好,我没有收到任何错误,但图像不在它应该在的文件夹中。

程序应该如何工作的简要总结:

  1. 用户使用 UIImagePicker 从他们的照片库中选择一张图片

  2. UIImageview 使用用户选择的 UIImage 进行更新

  3. 用户按下调用 IBAction“uploadImage”的“上传”按钮

  4. uploadImage 函数会将 UIImage 转换为 NSData 并将其发送到 PHP

  5. PHP 会将图片上传到所选文件夹

IBAction“uploadImage”的代码

-(IBAction)uploadImage{

//this is the url where the PHP file is located
NSString *urlString = @"http://localhost/~andy/uploadimage.php";

//Convert selected UIImage to NSData
NSData *imageData = UIImageJPEGRepresentation(chosenImage,.3);

if (imageData != nil)
{
    NSString *filenames = [NSString stringWithFormat:@"imageUploaded"];      

    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 *body = [NSMutableData data];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    //I use a method called randomStringWithLength: to create a unique name for the file, so when all the aapps are sending files to the server, none of them will have the same name:
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n",[self randomStringWithLength:10]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"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];

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    //Run code async 
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         NSData *returnData = data;
         NSLog(@"data recieved!%@", returnData);

         //Do what you want with your return data.   
     }];
}
}

这是uploadimage.php的php代码:

<?php

$target_path = "/";  //where I want the uploaded images to go
$target_path = $target_path . basename( $_FILES['userfile']['name'] );

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) {

    echo "Image uploaded";
} 
else {

    echo "Upload Failed";
}
?>

我想要存储图片的目录是本地本地目录uploadimagep.php所在的localhost/~andy/

运行程序后的结果

一切都返回成功,但我的 localhost/~andy/ 文件夹中没有图像

有谁知道我做错了什么?我觉得我在某个地方犯了一个菜鸟错误。

提前致谢!

【问题讨论】:

    标签: php xcode upload nsurlconnection


    【解决方案1】:

    这是一个例子:

    #define DataDownloaderRunMode @"myapp.run_mode" 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url 
                                   cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                   timeoutInterval:60]; 
    [request setHTTPMethod:@"POST"]; 
    // We need to add a header field named Content-Type with a value that tells that it's a form and also add a boundary.
    // I just picked a boundary by using one from a previous trace, you can just copy/paste from the traces.
    NSString *boundary = @"----WebKitFormBoundarycC4YiaUFwM44F6rT";
    
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    // end of what we've added to the header
    // the body of the post
    NSMutableData *body = [NSMutableData data];
    // Now we need to append the different data 'segments'. We first start by adding the boundary.
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    // Now append the image
    // Note that the name of the form field is exactly the same as in the trace ('attachment[file]' in my case)!
    // You can choose whatever filename you want.
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"attachment[file]\";
    filename=\"picture.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    // We now need to tell the receiver what content type we have
    // In my case it's a png image. If you have a jpg, set it to 'image/jpg'
    [body appendData:[[NSString stringWithString:@"Content-Type: image/png\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    // Now we append the actual image data
    [body appendData:[NSData dataWithData:imageData]];
    // and again the delimiting boundary    
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    // adding the body we've created to the request
    [request setHTTPBody:body];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request 
                                                           delegate:self 
                                                           startImmediately:NO]; 
    [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:DataDownloaderRunMode]; 
    [connection start];
    

    【讨论】:

      猜你喜欢
      • 2021-09-29
      • 2010-12-28
      • 1970-01-01
      • 2012-09-28
      • 2018-05-28
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-08
      相关资源
      最近更新 更多