iphone:使用 ASIFormDataRequest 上传图片到服务器 [英] iphone: upload image to server using ASIFormDataRequest

查看:25
本文介绍了iphone:使用 ASIFormDataRequest 上传图片到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须像这样使用 NSMutableURLRequest 将图像上传到我为其编写代码的服务器

I have to upload an image to server for which I wrote code using NSMutableURLRequest like this

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:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="userfile"; filename="ipodfile%@.jpg"
",self.fileID] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream

"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

我猜我的 PHP 脚本响应于此并且工作正常

I guess my PHP script respond corresponding to this and is working fine

如何在 ASIFormDataRequest 中复制上述内容?

how can I replicate above in 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:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="userfile"; filename="ipodfile%@.jpg"
",self.fileID] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream

"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

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

但是没有成功?

这是我的 PHP 脚本的详细信息

here are the details of my PHP scripts

<?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}";
}

?>

我按照 Cyprian 做的

I did as per Cyprian

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]"];

推荐答案

使用 ASIFormDataRequest 上传文件到服务器

Uploading file to server using 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]); 
}

注意我是凭记忆打字的,所以你可能有一些拼写错误.

Note I was typing from memory so you may have some misspellings.

这篇关于iphone:使用 ASIFormDataRequest 上传图片到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆