将图像文件上传到服务器时出错
我想使用Web服务将所选图像从图像查看器上载到服务器。将图像文件上传到服务器时出错
我的web服务需要用户ID,密码和文件名作为请求。
但是当我运行时,我得到错误的文件名。
错误显示“过程需要未提供的文件名”。虽然我在这里传递文件名。
任何人都可以看看代码,让我知道,可能是什么问题? - 谢谢。
NSData *imageData;
imageData = UIImageJPEGRepresentation(imageView.image, 90);
//creating the url request:
NSURL *postUrl = [NSURL URLWithString:@"myURL"];
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postUrl];
//adding header information:
[postRequest setHTTPMethod:@"POST"];
NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
//setting up the body:
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"100"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"pwd\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"pwd"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image\"; filename=\"1.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Type: image/jpg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[NSData dataWithData:imageData]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBody:postBody];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:nil error:nil];
NSString *returnString;
returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
lblLoginStatus.text = @"Uploading done.";
NSLog(@"%@",returnString);
在你的其他领域,你有你的格式字符串两次\r\n
,在文件名输入唯一的一个。也许这就是问题所在。
感谢您的帮助。我尝试过,但没有帮助。我看到了同样的错误。 –
我几乎可以肯定这是造成问题的线。我不确定我是否理解用';'分隔参数的格式。我建议检查你的网页表单的逻辑。 – Mundi
试试这个:虽然它的上传音频但应该以相同的方式工作。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
destinationFilePath = [[NSString alloc] initWithFormat: @"%@/audio.mp3", documentsDirectory];
progress.myLabel.text = @"Uploading file...";
[self.view addSubview:progress.view];
NSLog(@"will upload file: %@", destinationFilePath);
NSString * text = captionText;
//this one works
// try this with dataWithContentOfURL
NSData *audioData = [[NSData alloc] initWithContentsOfURL:fileURL];
NSLog(@"File URL:%@",fileURL);
NSString * urlString = @"urlstring"
// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSArray *myArray = [destinationFilePath componentsSeparatedByString: @"/"];
NSString *fileName = (NSString*)[myArray lastObject];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"action\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"upload" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"code\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSString *strkCode = @"code";
[body appendData:[[NSString stringWithFormat:@"%@", strkCode] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:audioData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
if (![text isEqualToString:@""]) {
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"caption"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
// setting the body of the post to the reqeust
[request setHTTPBody:body];
[audioData release];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
receivedData = [[NSMutableData data] retain];
}
else {
NSLog(@"error connecting!");
}
[connection release];
}
感谢您的帮助。我试过这个代码。我有上传的URL字符串。 在你的代码中,什么是“destinationFilePath”? NSArray * myArray = [destinationFilePath componentsSeparatedByString:@“/”]; –
我只是花了几天的时间尝试将图片上传到服务器上,实际上有很多变量。这可能是服务器端而不是iOS端的问题。 我可以推荐几件事吗? 1.注释掉以下行: [postBody appendData:[[NSString stringWithString:@“Content-Type:image/jpg \ r \ n \ r \ n”] dataUsingEncoding:NSUTF8StringEncoding]]; 2.添加内容长度: [请求addValue:[NSString stringWithFormat:@“%d”,[body length]] forHTTPHeaderField:@“Content-Length”]; – John