2011-05-07 80 views
7

我已經上傳圖像到服務器,這是我寫的使用NSMutableURLRequest這樣iphone:使用ASIFormDataRequest

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 
// 
// /* 
// now lets create the body of the post 
// */ 
    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile%@.jpg\"\r\n",self.fileID] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[NSData dataWithData:imageData]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

我想我與此對應的PHP腳本響應代碼上傳圖片到服務器和工作正常

如何在ASIFormDataRequest中複製以上內容?

試圖做到這一點

ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL: 
           [NSURL URLWithString:photoUploadURLString]]; 


NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile%@.jpg\"\r\n",self.fileID] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[NSData dataWithData:imageData]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[request addData:body withFileName:filename andContentType:@"image/jpeg" forKey:@"snapshot[image]"]; 
request.uploadProgressDelegate = self.progressView; 
[request setDidFinishSelector:@selector(getFacebookPhotoFinished:)]; 

但沒有得到sucess?

這裏有我的PHP腳本

<?php 

$uploaddir = './uploads/'; 
$file = basename($_FILES['userfile']['name']); 
$uploadfile = $uploaddir . $file; 

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 
     echo "http://example.com/uploads/{$file}"; 
} 

?> 

我一樣使用ASIFormDataRequest

-(void)uploadFile{ 
     NSURL *url = [NSURL URLWithString: photoUploadURLString]; 

     ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 

     [request setUseKeychainPersistence:YES]; 
     //if you have your site secured by .htaccess 

     //[request setUsername:@"login"]; 
     //[request setPassword:@"password"]; 


     NSString *fileName = [NSString stringWithFormat:@"ipodfile%@.jpg",self.fileID]; 
     [request addPostValue:fileName forKey:@"name"]; 

     // Upload an image 
     NSData *imageData = UIImageJPEGRepresentation([UIImage imageName:fileName]) 
     [request setData:imageData withFileName:fileName andContentType:@"image/jpeg" forKey:@"userfile"]; 

     [request setDelegate:self]; 
     [request setDidFinishSelector:@selector(uploadRequestFinished:)]; 
     [request setDidFailSelector:@selector(uploadRequestFailed:)]; 

     [request startAsynchronous]; 
} 

- (void)uploadRequestFinished:(ASIHTTPRequest *)request{  
    NSString *responseString = [request responseString]; 
     NSLog("Upload response %@", responseString); 
} 

- (void)uploadRequestFailed:(ASIHTTPRequest *)request{ 

     NSLog(@" Error - Statistics file upload failed: \"%@\"",[[request error] localizedDescription]); 
} 

注意每塞浦路斯

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"images/ipodfile%@.jpg", fileID]]; 
NSData *imageData = UIImageJPEGRepresentation([itemImageView image], 0.05); 
if (imageData != nil) { 
    [imageData writeToFile:savedImagePath atomically:YES]; 
} 
if ([[NSFileManager defaultManager] createFileAtPath:savedImagePath contents:imageData attributes:nil]) 
{ 
    NSLog(@"Image saved"); 
} else { 
    NSLog(@"Image not saved"); 
}   


[activityIndicator startAnimating]; 
NSString *photoUploadURLString = @"http://example.com/imageuploader.php"; 

NSString* filename = [NSString stringWithFormat:@"ipodfile%@.jpg", self.fileID]; 
NSLog(@"url is %@/%@",photoUploadURLString, filename); 

UIImage *newImage = [[UIImage alloc] init]; 
newImage=[itemImageView image]; 
NSURL *url=[NSURL URLWithString:photoUploadURLString]; 

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
[request setPostValue:@"userfile" forKey:@"name"]; 
[request setPostValue:filename forKey:@"filename"]; 

[request setData:imageData withFileName:filename andContentType:@"image/jpeg" forKey:@"snapshot[image]"]; 
+0

你能更具體地指出哪些問題? – 2011-05-07 21:24:06

+0

我的問題很簡單,我如何使用ASIHTTP請求發佈圖像,以便上面提到的PHP腳本可以保存圖像以上傳目錄 – 2011-05-07 21:34:47

+0

沒有錯誤?服務器沒有響應?很難說這裏有什麼問題。提供一些更多細節,我並不是說我們可以猜測代碼中出了什麼問題。 – 2011-05-07 21:37:26

回答

13

上傳文件到服務器我打字的細節從記憶中,所以你可能會有一些拼寫錯誤。

+0

對我不起作用 – 2011-05-07 23:09:46

+0

如果你澄清以下內容,它將起作用:你想發送什麼參數?告訴我的內容和密鑰對是這樣的:'文件名爲鍵「名稱」'等等 – Cyprian 2011-05-07 23:11:58

+0

在我的問題中看到我的輸入編輯 – 2011-05-07 23:19:03

2
NSString *strURL = @"enter uerl hear..."; 

//  ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]]; 
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]]; // Upload a file on disk 
NSString *filename1=[NSString stringWithFormat:@"friendship.jpg"]; 
UIImage *image1=[UIImage imageNamed:filename1]; 
NSData *imageData1=UIImageJPEGRepresentation(image1, 1.0); 
[request setData:imageData1 withFileName:filename1 andContentType:@"image/jpeg" forKey:@"avatar"]; 
[request setRequestMethod:@"POST"]; 
//[request appendPostData:body]; 
[request setDelegate:self]; 
[request setTimeOutSeconds:3.0]; 
request.shouldAttemptPersistentConnection = NO; 
[request setDidFinishSelector:@selector(uploadRequestFinished:)]; 
[request setDidFailSelector:@selector(uploadRequestFailed:)]; 
[request startAsynchronous]; 


- (void)uploadRequestFinished:(ASIHTTPRequest *)request 
{ 
NSLog(@" Error - Statistics file upload failed: \"%@\"",[request responseString]); 
} 

- (void)uploadRequestFailed:(ASIHTTPRequest *)request{ 
NSLog(@" Error - Statistics file upload failed: \"%@\"",[[request error] localizedDescription]); 

} 
+0

你的代碼工作得很好,我怎麼通過文本參數與文件之一,當我嘗試我得到異常+ 1 – RollRoll 2013-08-02 20:15:06

+0

如何使用相同的參數將多個圖像上傳到服務器? – IKKA 2013-10-05 16:09:50

相關問題