0

我正在尝试将一些图像文件从 iPhone 上传到我使用 NSURLConnection 使用 sendSynchronousRequest 发布数据的 Web 服务器。

我正在通过附加下面给出的几个字符串来准备我的身体。

NSString *check = @"Content-Disposition: form-data; name=\"userfile\"; filename=";
check = [check stringByAppendingString:@"\""];
check = [check stringByAppendingString:randomNumber];
check = [check stringByAppendingString:@".jpg"];
check = [check stringByAppendingString:@"\""];
check = [check stringByAppendingString:@"'\r\n'"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:check] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:app.imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",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];

但是,当我尝试在服务器中获取图像文件时,它会在文件名之后显示带有“和”的正确名称。

4

2 回答 2

0

看起来您实际上是在代码的第 5 行和第 6 行添加“和”。将这些行替换为仅添加换行符的单行:

check = [check stringByAppendingString:@"\r\n"];

更好的方法是使用不会阻止主 UI 线程响应的异步调用。使用像AFNetworking这样的框架来获得更好的抽象级别。在 AFNetworking 中,使用可以更轻松地进行多部分帖子。

于 2013-10-01T07:57:21.370 回答
0

您在代码中添加此标志:

check = [check stringByAppendingString:@"\""];
check = [check stringByAppendingString:@"'\r\n'"];
于 2013-10-01T07:46:55.860 回答