在 iPhone 上将文件上传到 FTP 服务器 [英] Upload File to FTP Server on iPhone

查看:46
本文介绍了在 iPhone 上将文件上传到 FTP 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现将文件上传到 FTP 服务器的逻辑.

I would like to implement logic that uploads file to FTP Server.

我是 iPhone 开发的新手,所以想找出首选方法.浏览此任务的可用 API 后,我设法仅找到使用 CFNetwork 的代码(看起来不是基于 Objective C).

I am new in iPhone development, so want to figure out preferred approach. After browsing available API for this task I have managed to find only code that uses CFNetwork (that looks like not Objective C based).

还有使用基于 Objective C 的 NSURL 等的 URL 加载系统.

There is also URL Loading System that uses NSURL etc which are Objective C based.

那么,我的问题是:是否可以使用URL Loading System来实现文件上传到FTP Server?

So, my question: Is it possible to use URL Loading System to implement file upload to FTP Server?

谢谢.

推荐答案

我使用 PHP 页面来发布文件并让 PHP 处理上传....

I use a PHP Page to post a file to and have PHP handle the uploading....

此代码用于上传照片,但它可以适用于任何文件.

This code is used to upload a photo, but it can be adapted to work with any file.

PHP 代码:

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

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "OK";
} else {
    echo "ERROR";
}

?>

iPhone 代码:

- (BOOL)uploadImage:(NSData *)imageData filename:(NSString *)filename{


    NSString *urlString = @"http://www.yourdomainName.com/yourPHPPage.php";

    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:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name="userfile"; filename="%@"
",filename]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream

"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];

    return ([returnString isEqualToString:@"OK"]);
}

方法调用:

[self uploadImage:UIImageJPEGRepresentation(imageView.image, 1.0) filename:imageName];

这篇关于在 iPhone 上将文件上传到 FTP 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆