本文介绍了将图像上传到服务器,iOS无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将图片上传到我的php服务器,但它无法正常工作。当我尝试上传文件时,屏幕只是冻结(这是预期的 - 我正在使用sendSynchronousRequest,直到我可以让它工作)大约一分钟,然后它返回一个空字符串作为响应。我已经在这方面工作了一天多,似乎无法让它发挥作用。此外,图像从未实际上传到服务器(我检查过)。提前致谢!
I am trying to upload an image to my php server but it's not working. When I try to upload the file, the screen just freezes (this is expected - I'm using sendSynchronousRequest until I can get this to work) for about a minute and then it returns an empty string as a response. I've been working on this for over a day and can't seem to get it to work. Furthermore, the image never actually uploads to the server (I checked). Thanks in advance!
// Uploading the image
-(IBAction)uploadImage:(id)sender{
[self uploadImage:UIImageJPEGRepresentation(productImageView.image, 90) filename:@"image.jpg"];
}
- (BOOL)uploadImage:(NSData *)imageData filename:(NSString *)filename{
// This isn't actually my url btw
NSString *urlString = @"http://www.mysite.com/uploadImage.php";
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
/*
add some header info now
we always need a boundary when we post a file
also we need to set the content type
You might want to generate a random boundary.. this is just the same
as my output from wireshark on a valid html post
*/
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"rn" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-streamrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Responce"
message:returnString
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
return ([returnString isEqualToString:@"OK"]);
}
我的php文件:
<?php
$uploaddir = ''; // Put in same directory as PHP file for debugging purposes
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "Your file is called {$file}";
}
?>
推荐答案
这肯定会有效。
这篇关于将图像上传到服务器,iOS无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!