我怎样才能使用NSURLConnection的上传文件和其他参数(How can i upload

2019-06-25 11:47发布

我使用NSURLConnection ,以相同的数据(发送NSString )到服务器,我想与他们添加图片或文件有啥内容类型的值?

编码

- (NSData *)encodingData:(NSMutableDictionary *)dictionary
{
    NSMutableArray *arrayPosts = [[NSMutableArray alloc] init];
    [dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
         NSString *encodedKey = [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
         NSString *encodedValue = [obj stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [arrayPosts addObject:[NSString stringWithFormat:@"%@=%@",encodedKey,encodedValue]];
    }];
    NSString *encodedArrayPosts = [arrayPosts componentsJoinedByString:@"&"];
    return [encodedArrayPosts dataUsingEncoding:NSUTF8StringEncoding];
}

发送数据

- (void)startAsyncRequest
{
   // Enable the network activity indicator in the status bar
   [self enableActivityIndicatorInStatusBar];

   // Setting of the request
   NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:self.url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
   [urlRequest setHTTPMethod:self.method];
   [urlRequest setHTTPBody:[self encodingData:self.dictionaryPosts]];

   // Send the request
   NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

   if (connection) {
      // Connection succeded 
     self.receiveData = [NSMutableData data];
   } else {
      // Connection Failed
      self.error = @"Connection Failed";
      // Inform the user that the connection failed
      [self.delegate requestFailed:self];
   }
}

Answer 1:

您需要将数据传递到作为流型服务器

public InsertImage(Stream stream)

类似的东西,然后解析流类型。

这是如何使用一些界限来构建你的要求

- (NSURLRequest *)buildRequest:(NSData *)paramData fileName:(NSString *)name {
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    [request setURL:self.url];
    [request setHTTPMethod:self.method];

    NSString *boundary = @"0xKhTmLbOuNdArY";
    NSString *endBoundary = [NSString stringWithFormat:@"\r\n--%@\r\n", boundary];

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

    NSMutableData *tempPostData = [NSMutableData data]; 
    [tempPostData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // Sample Key Value for data
    [tempPostData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"Key_Param"] dataUsingEncoding:NSUTF8StringEncoding]];
    [tempPostData appendData:@"Value_Param"] dataUsingEncoding:NSUTF8StringEncoding]];
    [tempPostData appendData:[endBoundary dataUsingEncoding:NSUTF8StringEncoding]];

    // Sample file to send as data
    [tempPostData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", name] dataUsingEncoding:NSUTF8StringEncoding]];
    [tempPostData appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [tempPostData appendData:paramData];
    [tempPostData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:tempPostData];
    return request;
}

你应该在你的服务器端的流式数据,并分析它,你可以让你发送文件对关键基础价值的传递如Key_Param和名称(文件名)。



文章来源: How can i upload file and other parameter using NSURLConnection