我正在尝试将一些图像文件从 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];
但是,当我尝试在服务器中获取图像文件时,它会在文件名之后显示带有“和”的正确名称。