Xcode将文件(图像)上传到FTP站点

问题描述 投票:0回答:1

我想将文件,图像上传到网站。 我已经阅读了Apple的SimpleFTPSample。 它可以与TestPhotos一起使用,但是太复杂了。 有人可以发布一个简单的代码,我可以在其中放置我的URL吗? 我使用了BlackRaccoon和ASIHTTPRequest,但是它不起作用。 谢谢

objective-c xcode ftp
1个回答
0
投票

对我来说最简单的方法是编写一个小的php脚本,然后将文件上传到该脚本。 的PHP不是那么复杂,但它的作品

请务必将网址替换为您的网站名称

upload.php

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

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "transfer successful at:   http://YOURWEBSITE.COM/{$uploaddir}/{$file}";
}

?>

目标文件

    NSString *imageString = [NSString stringWithFormat:@"Content-Disposition: form-data;    name=\"userfile\"; filename=\"%@\"\r\n", YOURIMAGE];




     NSData *imageData = UIImageJPEGRepresentation(newimageGlobal, 0.4);
    NSString *urlString = @"http://YOURWEBSITE.COM/upload.php";

    NSLog(@"upload url%@", urlString);

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [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:imageString ]     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];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
© www.soinside.com 2019 - 2024. All rights reserved.