iphone SDK : Upload image from iphone to a php Server send empty file ?(sample code link inside)

回眸只為那壹抹淺笑 提交于 2019-12-03 16:17:07
dhii

use HTTP multipart POST or ASIHTTPRequest


@WebberLai

Save the image to home directory:
 NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.jpg"];
    UIImage *img = pickerImage;


// Write a UIImage to JPEG with minimum compression (best quality) 
    [UIImageJPEGRepresentation(img, 0.5) writeToFile:jpgPath atomically:YES];

    NSString *photoPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.jpg"];

    request = [ASIFormDataRequest requestWithURL:webServiceUrl];
    [request setPostValue:requestString forKey:@"data"];
    [request setFile:photoPath forKey:@"apiupload"];
    [request startSynchronous];

It's fine to add this

NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.jpg"]; UIImage *img = pickerImage;

[UIImageJPEGRepresentation(img, 0.5) writeToFile:jpgPath atomically:YES];

But When I try to put in my button action

NSString *photoPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.jpg"];

request = [ASIFormDataRequest requestWithURL:webServiceUrl];
[request setPostValue:requestString forKey:@"data"];
[request setFile:photoPath forKey:@"apiupload"];
[request startSynchronous];

It got many error message ,I think is delegate problem

first complier says "request undeclared" and "ASIFormDataRequest undeclared"

and "requestString undeclared"

I try to give request and requestString declared

So I use ASIFormDataRequest *request; and NSString *requestString;

It clear an error about "equestString undeclared"

I don't know how to declared ASIFormDataRequest so it still a error...

Did I right to put this in IBAction ???

I look some example here

Thanks again~you are a really nice guy !

More Edit :

OK, Here is more step about how to import necessary .h .m and framework

download here

and import this header :

ASIHTTPRequestConfig.h

ASIHTTPRequestDelegate.h

ASIProgressDelegate.h

ASICacheDelegate.h

ASIInputStream.h

ASIInputStream.m

ASIHTTPRequest.h

ASIHTTPRequest.m

ASIFormDataRequest.h

ASIFormDataRequest.m

ASINetworkQueue.h

ASINetworkQueue.m

ASIDownloadCache.h

ASIDownloadCache.m

iPhone projects must also include:

ASIAuthenticationDialog.h

ASIAuthenticationDialog.m

Reachability.h (in the External/Reachability folder)

Reachability.m (in the External/Reachability folder)

than import Framework:

CFNetwork

SystemConfiguration.framework

MobileCoreServices.framework

CoreGraphics.framework

libz.1.2.3.dylib

OK,Here is a small bug between iPhone3 & iPhone4/touch4

@ dhil's Answer is almost right , You just need to give a right key (depends on Server)

[request setPostValue:requestString forKey:@"data"];
[request setFile:photoPath forKey:@"apiupload"];

Ok...now if you use iPhone3 , and you don't give requestString alloc

It runs fine ~

But if you run on iPhone4/touch4 ,It will cause EXC_BAD_ACCESS

even both device is iOS 4.1

So...Hope this answer will help you to save time about upload image to server.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!