i want to upload an
UIImage
to a server.
the problem i dont know how to add it to my post to the server.
until now i sent post with thia:
NSString *reqURL = [NSString stringWithFormat:#"url"];
reqURL = [reqURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:reqURL]];
NSURLResponse *resp = nil;
NSError *err = nil;
NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];
there is any different way to do for image?
Use ASIHttpRequest
-(void)uploadImage{
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:UIImageJPEGRepresentation(myUIImage) withFileName:#"myphoto.jpg" andContentType:#"image/jpeg" forKey:#"photo"];
[request setDelegate:self];
[request setDidFinishSelector:#selector(uploadRequestFinished:)];
[request setDidFailSelector:#selector(uploadRequestFailed:)];
[request startAsynchronous];
}
This answer points to this wrapper framework that should make it easy to do what you want without dealing with HTTP header strings and so on.
Basically, you'll turn your UIImage into an NSData object using a function like UIImagePNGRepresentation() and then upload it over HTTP.
Related
uploading HDvideo to the server using ASIFormDataRequest.
but it taking long time to upload.
my code is
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
if (movieUrl != Nil) {
NSData *videoData = [NSData dataWithContentsOfURL:movieUrl];
[request addData:videoData withFileName:[movieUrl lastPathComponent] andContentType:#"audio/mp4" forKey:#"video"];
[request setRequestMethod:#"POST"];
//
[request setTimeOutSeconds:600];
[request setDelegate:self];
//
[request setUploadProgressDelegate:progressView];
[request startSynchronous];
where movieurl is url return from imagepickerdidfinish
movieUrl = (NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
Well you are doing All of this synchronously which means your interface is blocked and seem unresponsive. I would recommend using asynchronous connection which will be way more responsive, executing on a separate thread and not blocking the main thread. Use:
[request startAsynchronous]
I am developing an application in which i want to post
xml data as request but i am not able to post it correctly ,i think.
My request xml data is
<loginRequest><username>101</username></loginRequest>
and my request is as follows :
`NSString *post=#"<loginRequest><username>101</username></loginRequest>";
NSURL *url = [NSURL URLWithString:#"my url"];
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
NSMutableData *mutData=[[NSMutableData alloc]init];
[request addRequestHeader:#"Content-Type" value:#"text/xml"];
[request setPostValue:#"test" forKey:#"body"];
[request setCompletionBlock:^{
NSData *data=[request responseData];
NSString *response=[request responseString];
}];
[request setFailedBlock:^{
NSLog(#"Failed");
}];
[request startAsynchronous];
`
Kindly help me with this..
You can check with your server api whether it supports different response type..
Accordingly you can set "Accept" parameter of HTTP request header.
[request addRequestHeader:#"Accept" value:#"application/xml"];
I am new to iPhone. I want to upload an image to a server. I have url
http://lifestander.com:8080/fileupload/uploadfile.html?username=tom&password=1234&directory=ebay&filename=fantacy.jpg
I use the following method for uploading the file, but the result in console is: {"success":"false", "message":"Authentication Failed"}
I do not understand the output in the console. So please tell me what do I do wrong, or improve my code. What to do?
Thanks.
-(void)uploadFile{
UIImage* theImage = [UIImage imageNamed:#"images.jpg"];
NSString* path = [[NSBundle mainBundle] pathForResource:#"images" ofType:#"jpg"];
NSString *photoUploadURLString=#"http://lifestander.com:8080/fileupload/uploadfile.html";
NSURL *url = [NSURL URLWithString: photoUploadURLString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setUseKeychainPersistence:YES];
// Upload an image
NSData *imageData = UIImageJPEGRepresentation(theImage,1.0);
[request addPostValue:#"tom" forKey:#"username"];
[request addPostValue:#"1234" forKey:#"password"];
[request addPostValue:#"ebay" forKey:#"directory"];
[request setData:imageData withFileName:#"images.jpg" andContentType:#"image/jpg" forKey:#"filename"];
[request setDelegate:self];
[request setDidFinishSelector:#selector(uploadRequestFinished:)];
[request setDidFailSelector:#selector(uploadRequestFailed:)];
[request startAsynchronous];
}
I'm not sure whether it's a code fault, since there is no crash and you receive a response. {"success":"false", "message":"Authentication Failed"} means that there is something wrong with the authentication. Check if there is any login/password/acces key required to upload your file and if yes, implement it.
I am getting a problem when i request to the server to upload and UImage
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/image.jpg"]; //UIImage *img = pickerImage;
[UIImageJPEGRepresentation(postingImage, 0.5) writeToFile:jpgPath atomically:YES];
NSData *imageData = UIImageJPEGRepresentation(postingImage, 0.5);
NSURL *url = [NSURL URLWithString:serverURL];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setData:imageData withFileName:#"image.jpg" andContentType:#"image/jpeg" forKey:#"photo"];
[request startSynchronous];
NSLog(#"responseStatusCode %i",[request responseStatusCode]);
NSLog(#"responseStatusString %#",[request responseString]);
it is giving this responses
responseStatusCode 200
responseStatusString Error: No file uploaded
Do any one know about this, where i am doing mistake,
your help is much appreciated.
Thank you
Instead of
[request setData:imageData withFileName:#"image.jpg" andContentType:#"image/jpeg" forKey:#"photo"];
Try as
[request addData:imageData withFileName:#"image.jpg" andContentType:#"image/jpeg" forKey:#"photo"];
I did same with help of ASIHTTPRequest and get images successfully posted on server. This should work I guess as it is working in one of my live project.
I want to upload large video file on server.I have a code that works fine for small videos but even 30 seconds video are also not uploaded.I have increased the timeOutSeconds also but that also of no use.
strurl contains the path of the video file.
Here's the code:-
NSString *str = [NSString stringWithFormat:#"%#/uploadVideo.php",appUrl];
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:self.gameID forKey:#"gameId"];
[request setFile:strurl forKey:#"uploadfile"];
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request setTimeOutSeconds:1000];
[request startSynchronous];
NSLog(#"responseStatusCode %i",[request responseStatusCode]);
NSLog(#"responseString %#",[request responseString]);
Please help me
Thanks in advance!!