2012-07-20 142 views
0

我是新来的iPhone。我想将图像上传到服务器。我有网址上传图片在服务器上iPhone

http://lifestander.com:8080/fileupload/uploadfile.html?username=tom &密码= 1234 &目录=易趣&名= fantacy.jpg

我用下面的方法来上传文件,但在控制台中的结果是:{“success”:“false”,“message”:“Authentication Failed”}

我不明白控制台中的输出。所以请告诉我我做错了什么,或者改进我的代码。该怎么办?

谢谢。

-(void)uploadFile{ 

UIImage* theImage = [UIImage imageNamed:@"images.jpg"]; 
NSString* path = [[NSBundle mainBundle] pathForResource:@"images" ofType:@"jpg"]; 



NSString *[email protected]"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]; 



} 

回答

2

我不知道它是否是一个代码错误,因为没有崩溃,您会收到一个回应。 {"success":"false", "message":"Authentication Failed"}表示authentication有问题。检查是否有任何登录/密码/访问密钥上传你的文件,如果是,执行它。

+0

感谢您的快速回复。我想问我我做得正确吗? – 2012-07-20 12:39:01

+0

您的代码是正确的,我以类似的方式使用ASIFormDataRequest。错误必须在这里:'[request addPostValue:@“tom”forKey:@“username”]; [request addPostValue:@“1234”forKey:@“password”];'。你确定这是正确的登录数据吗? – akashivskyy 2012-07-20 12:46:55

+0

是的。用户名和密码是正确的。请看看我在NSString * photoUploadURLString中使用的网址。这是正确的,或者我使用完整的网址? – 2012-07-20 13:08:08