Installing the ASIHTTPRequest Message on the Python SimpleHTTPServer Server?

I am working on a project (which I will not release to the application store - just for fun) that will upload the image via an HTTP request from my iPhone to the server on which I run the Python script SimpleHTTPServer (http://ubuntuguide.net/http- server-support-uploading-files-from-windows-in-ubuntu). I have successfully used the ASIHTTP API in the past for text strings, but I couldn’t figure out how to upload an image for life. This is what I am using now:

-(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];

}

Indeed, the only part that I suppose I'm wrong is:

[request setFile:[UIImage imageNamed:@"Image.png"] withFileName:@"Image.png" andContentType:@"image/png" forKey:@"file"];

Any thoughts on where I might be wrong?

+3
source share
4 answers

@SorinA. setFile:withFileName:andContentType:forKey:. UIImage.

, , setData:withFileName:andContentType:forKey:. UIImage NSData:

NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"Image.png"]);
[request setData: imageData
    withFileName: @"Image.png"
  andContentType: @"image/png"
          forKey: @"file"];

, URL- ( "mySimpleHTTPServer" ), , URL- "http://localhost: some_port/some_path"? , - Python .

+2

, , [request responseStatusCode] [request responseStatusMessage] [request responseString]. , . , 200 - , .

:

if(request.error) {
    NSLog("ERROR: %@",[error localizedDescription]);
}

.. ASIHTTPRequestConfig.h ASI, , , .

0 1 :

DEBUG_FORM_DATA_REQUEST 1
DEBUG_REQUEST_STATUS 1

, .

+1

ASIFormDataRequest?

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addData:imageData withFileName:@"mypicture.png" andContentType:@"image/png" forKey:@"photos"];
0

, setData setFile ? , "setFile" "setData". , .

, .

[request setFile: path withFileName: @ "image.png" andContentType: @ "image / JPEG" forKey: @ "file"];

the content type must be image / png.

to download the image, I use this code:

// Initilize Queue
[networkQueue setUploadProgressDelegate:statusProgressView];
[networkQueue setRequestDidFinishSelector:@selector(imageRequestDidFinish:)];
[networkQueue setQueueDidFinishSelector:@selector(imageQueueDidFinish:)];
[networkQueue setRequestDidFailSelector:@selector(requestDidFail:)];
[networkQueue setShowAccurateProgress:true];
[networkQueue setDelegate:self];

 NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"myImage.jpg"];
 url = [NSURL URLWithString:@"http://myserver/upload.php"];
 ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
 [request setPostValue:@"myImageName" forKey:@"name"]; 
 [request setFile:imagePath forKey:@"photo"];

 [networkQueue addOperation:request];
 [networkQueue go];

In addition, if you upload multiple photos at once, use setFile: forKey: instead of setData: forKey :. That way, ASIFormDataRequest will know that you are sending a fair bit of data, and it will transfer the request data from a temporary file to disk, which means that you do not need to store a lot of image data in memory.

0
source

Source: https://habr.com/ru/post/1794620/


All Articles