无法使用ASIHttpFormDataRequest --- iPhone上传视频

时间:2013-04-03 05:47:09

标签: iphone asihttprequest multipartform-data

我想以maultipartform的形式发出请求,格式为

<form action="http://xyz.com/web/video/formupload/01f7e4d2-9484-44ed-9e5d-bb4b7ff67739" method="post"
      enctype="multipart/form-data" ">
    <input id="uploadedFile" type="file" name="uploadedFile"/>
    <input type="hidden" name="token" value="TOKEN"/>
    <input type="submit" value="go" />
</form>

我正在为此目的使用asiformdatarequest类。 这是我的代码

ASIFormDataRequest *videoUploadRequest = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:[tokenValue objectForKey:@"url"]]];
    [videoUploadRequest setPostFormat:ASIMultipartFormDataPostFormat];
    [videoUploadRequest addData:[tokenValue objectForKey:@"token"] forKey:@"token"];
    [videoUploadRequest addData:video withFileName:@"New Video" andContentType:(@"video/*") forKey:@"uploadedFile"];

[videoUploadRequest startSynchronous];

NSError *error = [videoUploadRequest error];

if (!error)
{
    NSString *response = [videoUploadRequest responseString];
    NSLog(@"upload response: %@", response);
}
else
{
    NSLog(@"Eror -- %@", error);
}

我收到错误说

Eror -- Error Domain=ASIHTTPRequestErrorDomain Code=3 "Authentication needed" UserInfo=0x1e5ee090 {NSLocalizedDescription=Authentication needed}

请问任何人都可以判断代码是否有问题。 请求的格式或其他任何内容..

提前致谢!

1 个答案:

答案 0 :(得分:0)

得到了答案! 是以不正确的格式发送令牌。 在这里!

NSString *string = [NSString stringWithFormat:[tokenValue objectForKey:@"url"]]; 
NSURL *url = [NSURL URLWithString:string];

ASIFormDataRequest *videoUploadRequest = [ASIFormDataRequest requestWithURL:url];
[videoUploadRequest setPostFormat:ASIMultipartFormDataPostFormat];

[videoUploadRequest addPostValue:[tokenValue objectForKey:@"token"] forKey:@"token"];
[videoUploadRequest addData:video withFileName:@"uploadedFile" andContentType:(@"video/*") forKey:@"uploadedFile"];

[videoUploadRequest startSynchronous];

NSError *error = [videoUploadRequest error];


if (!error)
{
    NSString *response = [videoUploadRequest responseString];
    NSLog(@"upload response: %@", response);
}
else
{
    NSLog(@"Error -- %@", error);
}