- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
PHP 代码工作正常。我已经从同一台服务器上的 html 表单上传了文件。上传的文件从 40K 到 2.0M 不等,所以它不是大小。在运行 PHP 5.3 的服务器上激活文件上传
我发现了很多这样的帖子(还没有答案):https://stackoverflow.com/questions/19710388/using-nsurlsession-to-upload-an-image .
这个使用 uploadTaskWithRequest:fromFile: 而不是 fromData: NSURLSession make sure Upload worked
这是 NSURLSessionDataTask 而不是 uploadTaskWithRequest: No data receiving with NSURLSession
有些帖子似乎说 uploadTaskWithRequest:fromData: 根本不起作用: NSURLSession: uploading assets with background transfer asynchronous upload with NSURLSession will not work but synchronous NSURLConnection does和 Upload image from iOS to PHP
我制作的应用程序返回 HTTP 代码,我得到代码 200。上传后服务器 error_log 文件中没有任何内容。一切似乎都正常,但无论发生什么,文件都不会写入服务器。我还有什么想法可以尝试找出问题所在?
这是PHP代码:
<?php
$file='';
$uploaddir='';
////////////////
echo 'file count=', count($_FILES),"\n";
var_dump($_FILES);
echo "\n";
////////////////
if(isset($_FILES['userfile']['name'])){
$uploaddir = './photos/'; //Uploading to same directory as PHP file
$file = basename($_FILES['userfile']['name']);
echo "file is set";
$uploadFile = $file;
$randomNumber = rand(0, 99999);
$newName = $uploaddir . $uploadFile;
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
echo "Temp file uploaded. \r\n";
} else {
echo "Temp file not uploaded. \r\n";
}
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $newName)) {
$postsize = ini_get('post_max_size'); //Not necessary, I was using these
$canupload = ini_get('file_uploads'); //server variables to see what was
$tempdir = ini_get('upload_tmp_dir'); //going wrong.
$maxsize = ini_get('upload_max_filesize');
echo "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ;
}
}
?>
这是 iOS 代码:
- (void)uploadImage:(UIImage*)image {
// 0 Define URL
NSData *imageData = UIImageJPEGRepresentation(image, 0.6);
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.myserver.com/app/photos/uploadPhoto.php"]];
NSString *boundary = @"0xKhTmLbOuNdArY";
NSString *filename = [NSString stringWithFormat:@"someName.jpg"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
// 4 Create object to put content into...
NSMutableData *body = [NSMutableData data];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\" filename=\"%@\"\r\n",filename]] dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//Not used with this:https://stackoverflow.com/questions/20893171/asynchronous-upload-with-nsurlsession-will-not-work-but-synchronous-nsurlconnect/20896632?noredirect=1#comment39074802_20896632
//[request setHTTPBody:body];
__block NSString *stringForText = @"Hola";
///////////////////////////////////////////////////
// 3
self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// ...
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
int errorCode = httpResponse.statusCode;
NSString *errorStatus = [NSString stringWithFormat:@"%d",errorCode];
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *totalResponse = [errorStatus stringByAppendingString:responseString];
stringForText = totalResponse;
[self updateView:stringForText];
//UIAlertView *alertme = [[UIAlertView alloc] initWithTitle:@"error" message:totalResponse delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
//[alertme show];
// 4
self.uploadView.hidden = NO;
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}];
// 5
[_uploadTask resume];
}
-(void)updateView:(NSString*)texto{
dispatch_sync(dispatch_get_main_queue(), ^{
self.myTextView.text = texto;
});
}
我对该文件夹拥有 777 权限。正如我所说,我能够通过 html 表单从同一个 php 脚本上传并写入该文件夹。
对于 http 错误代码,totalResponse 为 200,对于 php 转储,count=0 以及 array(0) {}。
这是应用程序中服务器的响应图像:
我在回显上传的临时文件后添加了这些行:
$body = $HTTP_RAW_POST_DATA;
echo "\n REQUEST" . $body;
但我只得到了这个:
file count=1 array(1) { ["user file"]=> array(5) { ["name"]=> string(12) "DrinkCO2.png" ["type"]=> string(9) "image/png" ["tmp_name"]=> string(14) "/tmp/phpxg7oLZ" ["error"]=> int(0) ["size"]=> int(190550) } } file is setTemp file uploaded. REQUEST 190550 image/png
最佳答案
它不是缺少冒号,而是缺少分号。
"name=\"userfile\" : filename=\"%@\"\r\n""
应该是
"name=\"userfile\"; filename=\"%@\"\r\n""
至少这是您的代码对我有用的唯一方式(顺便说一句谢谢!)。
关于php - 为什么 NSURLSession uploadTaskWithRequest :fromData: fail to upload to php server?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25127058/
我对 swift 很陌生,我有以下代码 var data : AnyObject let dict = jsonObject as NSDictionary do {
来自网络编程领域,我非常擅长使用多部分表单请求来上传文件。然而,在 iOS 中,我们有一个名为 NSURLSession 的东西,它带有 uploadTaskWithRequest 方法,这似乎是调用
我想使用单个 uploadTaskWithRequest 方法在后台上传多张图片。在尝试以下代码时,后台 session 不支持从 NSData 返回上传任务...请问如何实现这一点 func cre
我正在尝试使用 NSURLSession 将图像上传到云存储。 let data = UIImageJPEGRepresentation(imageAttachment, 1) NSURLSessio
我正在尝试将以前使用 AFNetworking 2.6.3 的项目升级到 AFNetworking 3.1。 我有一个 URL 请求: NSURL *requestURL = [NSURL URLWi
我正在使用 swift 2 编写一个应用程序,我需要在后台 session 中将照片和视频上传到我的服务器上。出于这个原因,我写了这段代码: let configuration = N
PHP 代码工作正常。我已经从同一台服务器上的 html 表单上传了文件。上传的文件从 40K 到 2.0M 不等,所以它不是大小。在运行 PHP 5.3 的服务器上激活文件上传 我发现了很多这样的帖
我是一名优秀的程序员,十分优秀!