【问题标题】:Upload HTML file to web server in iOS将 HTML 文件上传到 iOS 中的 Web 服务器
【发布时间】:2012-07-06 22:54:09
【问题描述】:

我需要能够将通过我的 iPhone 应用程序生成的 HTML 文件上传到我的网络服务器。

我确信这是可能的,但是我该怎么做呢?

【问题讨论】:

    标签: html objective-c ios


    【解决方案1】:

    这是一些使用 iOS 上传图片的代码。更改要上传的文件类型并使其与 HTML 文件一起使用应该很简单。

    NSString *urlString = @"http://yourserver.com/upload.php";
    NSString *filename = @"filename";
    request= [[[NSMutableURLRequest alloc] init] autorelease];
    [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 *postbody = [NSMutableData data];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[NSData dataWithData:YOUR_NSDATA_HERE]];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:postbody];
    
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", returnString);
    

    取自earlier thread

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-07
      • 2017-12-09
      • 2014-04-04
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      相关资源
      最近更新 更多