1

我在将图像上传到服务器时遇到问题。并得到500 Internal Server Error.

我正在通过以下代码上传图片

ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
    [request setRequestMethod:@"PUT"];
    [request setPostValue:[[NSUserDefaults standardUserDefaults]valueForKey:@"nickname"] forKey:@"nick"];

    NSData *imageData = UIImagePNGRepresentation(img.image);
     [request addData:imageData withFileName:@"image.png" andContentType:@"image/png" forKey:@"image"];
    //[request setData:imageData withFileName:@"profileImage.jpeg" andContentType:@"image/jpeg" forKey:@"image"];

    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestSuccess:)];
    [request setDidFailSelector:@selector(requestFailed:)];
    [request startAsynchronous];

并作为回应

-(void)requestSuccess:(ASIFormDataRequest *)request{
    NSLog(@"%@",[request responseData]);
    NSError *error;

    NSString* newStr = [[[NSString alloc] initWithData:[request responseData]
                                          encoding:NSUTF8StringEncoding] autorelease];
    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:&error];

    NSLog(@"%@",error);
}

首先将数据JSON数据转换为错误NSdictionary,并通过以下错误

 Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x8563f70 {NSDebugDescription=Invalid value around character 0.}

然后我尝试检查是否Response像上面那样在 nsstring 中转换数据,这样做我得到低于字符串的响应

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
 <title>500 Internal Server Error</title>
 <h1>Internal Server Error</h1>
 <p>The server encountered an internal error and was unable to complete your request.    Either the server is overloaded or there is an error in the application.</p>

可能是什么问题?

4

1 回答 1

0

这个:

[request setRequestMethod:@"PUT"];

应该:

[request setRequestMethod:@"POST"];
于 2013-07-09T07:47:19.267 回答