将文件上传到服务器 - Objective C/Xcode/Mediawiki
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11537648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload A File Into A Server - Objective C/Xcode/Mediawiki
提问by kamalbhai
I want to upload a UIImage
into a server. For this I am using the following lines of code ::
我想上传UIImage
到服务器。为此,我使用以下代码行 ::
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
imageView.image = image;
NSData *pngData = UIImagePNGRepresentation(imageView.image);
NSString *imageFile = @"image.png";
NSString *docDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [docDir stringByAppendingPathComponent:imageFile];
NSString *dataIS=[NSString base64StringFromData:pngData length:[pngData length]];
[pngData writeToFile:filePath atomically:YES];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[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"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image\"; filename=\"image.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:pngData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
But, I am getting the following error in gdb ::
但是,我在 gdb 中收到以下错误::
Your request could not be processed. Request could not be handled
.
Your request could not be processed. Request could not be handled
.
I am also confused over the proper use of urlString
i.e. I am doubtful over my correct use of it.
我也对urlString
ie的正确使用感到困惑,我怀疑我正确使用它。
If I however use the following urlString
, I am able to upload my image file into the server ::
但是urlString
,如果我使用以下内容,则可以将图像文件上传到服务器 ::
NSString *urlString = [NSString stringWithFormat:@"http://xxxx.com/mediawiki/api.php?action=upload&filename=image.png&url=%@&token=%@", url, token] ;
With reference to the API
for mediawiki
(http://www.mediawiki.org/wiki/API:Upload), can someone help me to sort it out ?? Thanks and Regards.
参考API
for mediawiki
(http://www.mediawiki.org/wiki/API:Upload),有人可以帮我整理一下吗??感谢致敬。
采纳答案by Narasimhaiah Kolli
If you want to send image to server,send by data in encoded form,server can handle encoded form of image and they will get image data in encoded from.
如果你想将图像发送到服务器,通过编码形式的数据发送,服务器可以处理图像的编码形式,他们将从编码中获取图像数据。
回答by Krushankant
I have same problem to upload multiple images and data but i am use AFNetworking class and Upload multiple images into server.
我在上传多个图像和数据时遇到同样的问题,但我使用 AFNetworking 类并将多个图像上传到服务器。
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"your URL"]];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
path = [[NSString alloc] initWithString:[URL absoluteString]];
vdict = @{
// Your Parameter Pass
};
[self checkmultiple:vdict];
-(void)checkmultiple:(NSDictionary*)vdict
{
AFHTTPRequestOperation *op = [manager POST:path parameters:vdict constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
for(i = 0;i<[appDelegate.gblarrydata count];i++)
{
NSString *paths = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"New Folder"];
// NSLog(@"%@",paths);
NSString *documentsDirectory = paths;
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[[appDelegate.gblarrydata objectAtIndex:i]valueForKey:@"nameimage"]];
NSData *data1 =[[NSData alloc]initWithContentsOfFile:getImagePath];
NSString *data2 =[[appDelegate.gblarrydata objectAtIndex:i] valueForKey:@"Note"];
[formData appendPartWithFileData:data1 name:[NSString stringWithFormat:@"pimage%d",i+1] fileName:[NSString stringWithFormat:@"pimage%d.jpg",i+1] mimeType:@"image/jpeg"];
if(![data2 isEqualToString:@""])
{
[formData appendPartWithFormData:[data2 dataUsingEncoding:NSUTF8StringEncoding]
name:[NSString stringWithFormat:@"pnotes%d",i+1]];
}
}
}
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"Upload success");
NSString *returnString = [NSString stringWithFormat:@"%@",[responseObject JSONRepresentation]];
NSLog(@"Reg Date:%@",returnString);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error:%@",error);
}];
[op start];
}
回答by HDdeveloper
I had the same problem. I used ASIHTTPRequest, for uploading files, it works fine.
我有同样的问题。我使用 ASIHTTPRequest上传文件,它工作正常。
Uploading:
上传:
Imagefile:
图像文件:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:@"Ben" forKey:@"names"];
[request addPostValue:@"George" forKey:@"names"];
[request addFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photos"];
[request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg" forKey:@"photos"];
With Other data simultaneously:
与其他数据同时:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
// Upload a file on disk
[request setFile:@"/Users/ben/Desktop/ben.jpg" withFileName:@"myphoto.jpg" andContentType:@"image/jpeg"
forKey:@"photo"];
// Upload an NSData instance
[request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];
for Downloading to desired Path:
下载到所需路径:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:@"/Users/ben/Desktop/my_file.txt"];
回答by arturo acosta
// UIImage * img = [UIImage imageNamed:@"min.png"];
// UIImage * img = [UIImage imageNamed:@"min.png"];
// NSData *imageData = UIImageJPEGRepresentation(img,0.2);
// NSData *imageData = UIImageJPEGRepresentation(img,0.2);
NSString *strUrl =[NSString stringWithFormat:@"%@/upload_file.php", [globalApp sharedUser].vg_dominio ];
//strUrl = @"http://192.168.1.100/mismedicinas/upload_file.php";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strUrl]];
request.HTTPMethod = @"POST";
request.timeoutInterval = 60;
request.HTTPShouldHandleCookies = false;
//[request setHTTPMethod:@"POST"];
NSString *boundary = @"----------SwIfTeRhTtPrEqUeStBoUnDaRy";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
//[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
NSMutableData *tempData = [NSMutableData data];
[tempData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[tempData appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"iphoneimage.xml\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[tempData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//[tempData appendData:[NSData dataWithData:imageData]]; ///IMAGEN
[tempData appendData:[NSData dataWithData:cData]];
[tempData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData: tempData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue: [NSString stringWithFormat:@"%d", body.length ] forHTTPHeaderField:@"Content-Length"];
request.HTTPBody =body;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"finalizacion %@", returnString);