How to upload image using Ajax request?

How can I make an iOS ASIHTTPRequest work with a Python SimpleHTTPServer?

  • I am working on a project (that i will not be releasing to the app store - just for fun) that will upload an image via an HTTP Post request from my iPhone to a server that I have running the Python script SimpleHTTPServer(http://ubuntuguide.net/http-server-support-uploading-files-from-windows-in-ubuntu). I have successfully used the ASIHTTP APIs in the past for text strings, but can't for the life of me figure out how to upload an image. This is what I am currently using: -(void)processRequest {     ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"mySimpleHTTPServer"]];     [request setFile:[UIImage imageNamed:@"Image.png"] withFileName:@"Image.png" andContentType:@"image/png" forKey:@"file"];     [request startSynchronous];     NSLog(@"%@",[request responseString]);     NSUserDefaults *profiles = [NSUserDefaults standardUserDefaults];     [profiles setObject:[request responseString] forKey:@"response"];     [profiles synchronize];     [self performSelector:@selector(endRequest) withObject:nil afterDelay:0]; } Really, the only part that I am assuming I am wrong about is the : [request setFile:[UIImage imageNamed:@"Image.png"] withFileName:@"Image.png" andContentType:@"image/png" forKey:@"file"]; Any thoughts on where i could be going wrong?

  • Answer:

    Try something like this: NSString *path = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"];  NSData *data = [NSData dataWithContentsOfFile:path];  [request setData:data withFileName:@"image.png" andContentType:@"image/png" forKey:@"file"]; //I haven't tried this code -- I just whipped it up so YMMV Edit: And it's probably a good idea to check for the existence of the file you try to read from disk as well. Also something like: [request setFile:path forKey:@"file"]; would likely be better since it looks like ASI would read it from disk as needed instead of just shoving it into memory all at once.

Amro Mousa at Quora Visit the source

Was this solution helpful to you?

Other answers

Wouldn't this question be better answered on Stack Overflow or by contacting Ben Copsey?  Try here... http://stackoverflow.com/ http://stackoverflow.com/search?q=ASIHTTPRequest+upload+images https://github.com/pokeb http://allseeing-i.com/ASIHTTPRequest/How-to-use http://twitter.com/#!/pokeb Been a while since I was on Quora, am surprised to see such technical programming questions being asked on here :) Hope this helps...

Joseph Carney

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.