通过AFNetworking上传图像

通过AFNetworking上传图像

我正在关注一些通过AFNetworking上传图像(数据)的教程。我已经做好了

 NSURL *url = [NSURL URLWithString:@"http://184.154.0.24"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

//    NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];
//    [_params setObject:multiMData forKey:@"Bytes"];
//    [_params setObject:name forKey:@"UserId"];
//    [_params setObject:@"Hello" forKey:@"fileName"];
//    [_params setObject:@"png" forKey:@"contentType"];

//    [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];

NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/IphoneVideoService/webservice.asmx/getStringData" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)
{
    [formData appendPartWithFileData:multiMData name:@"stringReturn" fileName:@"stringReturn.png" mimeType:@"image/png"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];


[operation setCompletionBlock:^{
    NSLog(@"%@", operation.responseString); //Lets us know the result including failures
}];

//    NSOperationQueue *queue = [[NSOperationQueue alloc] init] ;
//    [queue addOperation:operation];

[httpClient enqueueHTTPRequestOperation:operation];


编译后我有那个错误

2013-04-09 13:22:49.153 Softo Chat[582:19d03] Sent 323584 of 333526 bytes
2013-04-09 13:22:49.153 Softo Chat[582:19d03] Sent 327680 of 333526 bytes
2013-04-09 13:22:49.153 Softo Chat[582:19d03] Sent 331776 of 333526 bytes
2013-04-09 13:22:49.154 Softo Chat[582:19d03] Sent 333526 of 333526 bytes
2013-04-09 13:22:49.154 Softo Chat[582:1ad0b] System.InvalidOperationException: Request    format is invalid: multipart/form-data; boundary=Boundary+0xAbCdEfGbOuNdArY.
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()


及其服务器端编程asp.net

[WebMethod]
public string getStringData(string stringReturn)
{
    return stringReturn;
}

最佳答案

嗨,Pravi杰伊,我刚刚在Google上搜索了一个相关的好问题。您只需要对web.config进行一些更改。

<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />


请访问此:

'System.InvalidOperationException: Request format is invalid: multipart/form-data' error when posting image from iphone to .NET webservice

10-08 07:48