使用POST从iphone上传zip文件

时间:2013-02-04 16:27:28

标签: iphone file-upload file-io http-post

我正在尝试使用以下代码上传zip文件,图片和其他文件已成功上传但是zip没有成功,有什么想法吗?

- (IBAction)upload:(UIButton *)sender 
{

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

  NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.zip"]; 


  NSData * fileData = [[NSData alloc] initWithContentsOfFile:path];

这只是一个简单的网址,我的网址工作正常

NSString *urlString = [NSString stringWithFormat:@"http://xxxx.xxxx.com/fileupload.aspx"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

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



NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:fileData]];


// setting the body of the post to the reqeust
[request setHTTPBody:body];        

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

NSLog(@"return string: %@",returnString);

if([returnString isEqualToString:@"Success"])
{
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Upload" message:@"File successfully uploaded" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
}
else
{
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Upload" message:@"File upload failed" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
}
}

我做错了什么?

0 个答案:

没有答案