2011-12-12 2 views
1

내 응용 프로그램에서 서버에 이미지를 업로드하고 싶습니다.이미지를 서버에 동적으로 업로드하는 방법은 무엇입니까?

서버에 이미지를 업로드 할 수 있지만 이미지 이름에 filename = % @. jpg가 표시됩니다. 이미지

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

에 대한 동일하게 유지하지만 난 그것을 가지고 같은 이미지의 이름으로 서버에 이미지를 저장하려면 ...... 은 PLZ 날 도와.

다음

내 코드입니다 :

-(IBAction)upload:(id)sender 
{ 

    //imageview is a UIImageView 
    NSData *imageData = UIImageJPEGRepresentation(imageView.image, 100); 


    // setting up the URL to post to 
    NSString *urlString = @"http://url.com/upload/uploader.php?"; 

    // setting up the request object now 

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setHTTPMethod:@"POST"]; 

    /* 
    add some header info now 
    we always need a boundary when we post a file 
    also we need to set the content type 

    You might want to generate a random boundary.. this is just the same 
    as my output from wireshark on a valid html post 
    */ 
    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 stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n"] 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]]; 
    // setting the body of the post to the reqeust 
    [request setHTTPBody:body]; 

    // now lets make the connection to the web 

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    ///self.returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

    NSLog(@"%@",returnString); 


} 

답변

1

변화

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

파일 이름이있는 NSString가

[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n",filename] dataUsingEncoding:NSUTF8StringEncoding]]; 

에 * 원하는 파일 이름

+0

아니요 ... 작동하지 않습니다 ... 잠시 설명해주세요 ..... 감사합니다. –

0

이 줄 변수 [body appendData : [[NSString stringWithString : @ "콘텐츠 - 처리 : 양식 데이터; name = \ "userfile \"; 파일명 = \ ". %의 @ JPG의 \"\ R \ n "] dataUsingEncoding : NSUTF8StringEncoding];

콘텐츠 유형 : 이미지/JPEG

대신 내용 - 처리 : 폼 데이터

콘텐츠 유형을 사용하는 방법을 알아보기

관련 문제