繁体   English   中英

从iOS设备将图像上传到php服务器时出现问题?(500内部服务器错误)

[英]While uploading image from iOS device to php server issue?(500 Internal Server Error)

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
 misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator,
webmaster@domain.com and inform them of the time the error occurred,
and anything you might have done that may have
caused the error.</p>
<p>More information about this error may be available
in the server error log.</p>
<p>Additionally, a 500 Internal Server Error
error was encountered while trying to use an ErrorDocument to handle the request.</p>
 <hr>
<address>Apache Server at domain.com Port 80</address>
 </body></html>

iOS开发人员可以尝试将图像发送到PHP服务器。 但是他们遇到了上面的错误

我该如何解决这个问题?

是iOS代码问题还是PHP附带问题?

通过邮递员,我检查了: 在此处输入图片说明

通过使用此URL,他们向我发送了多张图像,但是如果是单张图像,则可以正常工作,但是尝试发送多张图像却不起作用: https : //charangiri.wordpress.com/2014/09/22/how-to-upload -多个图像到服务器/

您可以在下面使用此功能将多个图像上传到服务器。 我使用的imagesArray变量用于存储UIImage对象。 您可以根据需要进行更改。

- (void)uploadImagesToServer{
//create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

//Set Params
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];

//Create boundary, it can be anything
NSString *boundary = @"------friendlyWagonBoundary4QuqLuM1cE5lMwCy";

// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

// post body
NSMutableData *body = [NSMutableData data];

for (int i=0; i<[imagesArray count]; i++)
{
    UIImage *images=[imagesArray objectAtIndex:i];
    NSData* imageData = UIImageJPEGRepresentation(images, 0.7);

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image%d\"; filename=\"image%d\"\r\n", i+1,i+1] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type:image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

}
//Close off the request with the boundary
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the request
[request setHTTPBody:body];

// set URL
[request setURL:[NSURL URLWithString:@"http://example.com"]]; // Give your URL here.

[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                           NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
                           NSString *str = [NSString stringWithUTF8String:[data bytes]];
                           NSLog(@"str %@",str);
                           if ([httpResponse statusCode] == 200) {

                               NSLog(@"success");
                           }else{
                               NSLog(@"failed");
                           }

                       }];
}

希望这个能对您有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM