ios - 将两张图片上传到 WebServer
<p><p>我正在尝试将两张图片上传到我的网络服务器。这是我现在要做的,上传一张图片:</p>
<pre><code>NSData *imageData = UIImagePNGRepresentation(imageToSend);
// setting up the URL to post to
NSString *urlString = @"http://www.myweb.com.br/_resources/testeDir.php";
// setting up the request object now
NSMutableURLRequest *request = [ init];
];
;
NSString *boundary = @"14737809831466499882746641449";
NSString *contentType = ;
;
/*
now lets create the body of the post
*/
NSMutableData *body = ;
dataUsingEncoding:NSUTF8StringEncoding]];
];
];
];
dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
;
//Create the connection with the string URL and kick it off
NSURLConnection *urlConnection = ;
;
</code></pre>
<p>一张图片效果很好,但是,我怎么能同时发送两张图片呢?</p>
<p>一种选择是使用 AFNetworking,但到目前为止我无法让它工作,这是我尝试过的:</p>
<pre><code>AFHTTPClient *httpClient = [ initWithBaseURL:urlString];
NSMutableURLRequest *requestHTTP = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
;
}];
AFJSONRequestOperation *operation = [ initWithRequest:requestHTTP];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
//CGFloat progress = ((CGFloat)totalBytesWritten) / totalBytesExpectedToWrite * 100;
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Upload succes");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Upload failed");
}];
;
</code></pre>
<p>这是我的 php 代码:</p>
<pre><code><?php
$currentDirectory = getcwd();
$uploaddir = $currentDirectory."/4/HD/";
$uploaddir2 = $currentDirectory."/4/thumb/";
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
$file2 = basename($_FILES['userfile2']['name2']);
$uploadfile2 = $uploaddir2 . $file2;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo $uploaddir."{$file}";
}
if (move_uploaded_file($_FILES['userfile2']['tmp_name'], $uploadfile2)) {
echo $uploaddir2."{$file2}";
}
?>
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>只需向请求中添加另一个名称不同的图像。</p>
<pre><code> NSMutableData *body = ;
dataUsingEncoding:NSUTF8StringEncoding]];
];
];
];
dataUsingEncoding:NSUTF8StringEncoding]];//MOD HERE
//and add
dataUsingEncoding:NSUTF8StringEncoding]];
];
];
];
dataUsingEncoding:NSUTF8StringEncoding]];
</code></pre>
<p>在添加两个文件之前不要关闭正文<em>--%--\r\n</em></p>
<p>[编辑]这就是我的做法:</p>
<pre><code>if([ fileExistsAtPath:path])
{
NSData *data = ;
//NSLog(@"image present:%d",);
dataUsingEncoding:NSUTF8StringEncoding]];
];
];
//];
;
];
}
if([ fileExistsAtPath:videoPath])
{
NSString *content = ;
NSURL *pathURL = ;
////NSLog(@"cur url is:%@",pathURL);
NSData *data = ;
//NSLog(@"data len is:%d",);
dataUsingEncoding:NSUTF8StringEncoding]];
];
];
;
];
}
// close form
dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
;
</code></pre></p>
<p style="font-size: 20px;">关于ios - 将两张图片上传到 WebServer,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/15660360/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/15660360/
</a>
</p>
页:
[1]