【问题标题】:Objective C post variables as well as filesObjective C 发布变量和文件
【发布时间】:2013-02-04 12:08:31
【问题描述】:

我正在尝试在目标 c 中编写一个方法/函数来将数据发布到在线 php 脚本。一切顺利,它成功上传了我提供的尽可能多的文件,但我无法弄清楚将变量与这些文件一起发送。我已经花了几个小时浏览这个网站,但找不到发布文件和变量的示例。

NSURL * postURL = [NSURL URLWithString:serverURL];
NSString * contentBoundary = @"-14737809831466499882746641449";
NSString * contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", contentBoundary];

// Build Request
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:postURL];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData * postBody = [NSMutableData data];

NSMutableString * postString = [NSMutableString stringWithFormat:@""];
for(NSString * key in dataArray) [postString appendString:[NSString stringWithFormat:@"%@=%@&", key, [dataArray objectForKey:key]]];
[postString deleteCharactersInRange:NSMakeRange([postString length] -1, 1)]; // Trim trailing ampersand from string
NSData * postData = [postString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];

[postBody appendData:[NSData dataWithData:postData]];

if(attachments != nil){
    for(NSString * filePath in attachments){
        NSString * fileName = [filePath lastPathComponent];
        NSData * attachedFile = [NSData dataWithContentsOfFile:filePath];
        [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", contentBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data;name=\"userfile[]\"; filename=\"%@\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[[NSString stringWithFormat:@"Content-Type:application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[NSData dataWithData:attachedFile]];
        [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", contentBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    }
}

NSString * postLength = [NSString stringWithFormat:@"%d", [postBody length]];
[urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setHTTPBody:postBody];

任何帮助将不胜感激。

【问题讨论】:

    标签: objective-c xcode ios6 nsmutableurlrequest


    【解决方案1】:

    您可以尝试通过以下方式将变量的值附加到发布的数据中:

    // text parameter
        [postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
        [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"applicationId\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        NSString *parameterValue1=[dict objectForKey:@"applicationId"];
        [postbody appendData:[parameterValue1 dataUsingEncoding:NSUTF8StringEncoding]];
        [postbody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    

    希望对你有用。以上代码只是文件上传时发送的参数示例。

    【讨论】:

    • 我现在检查一下并回复你:)
    • 当然。我会等着。 :)
    猜你喜欢
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 2010-11-16
    • 1970-01-01
    • 2011-11-30
    相关资源
    最近更新 更多