search

Home  >  Q&A  >  body text

iOS upload image as stream

The company uploads pictures. The server-side personnel said that pictures are uploaded through picture streaming. I tried the following methods but none of them worked:

[manager POST:@"http://xxxxxxxx:8094/interface_normal/editorFile/uploadImages.do" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    NSData* imageData = UIImageJPEGRepresentation(images[i], 0.5);
    [formData appendPartWithFileData:imageData name:@"file" fileName:@"tupian.png" mimeType:@"image/png"];
} success:^(NSURLSessionDataTask *task, id responseObject) {
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
    
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    
}];

There are also the following methods:

NSData *data = UIImageJPEGRepresentation(image, 0.5f);
NSString *uploadUrl = [NSString stringWithFormat:@"%@editorFile/uploadImages.do", BASE_URL];
NSInputStream *imageStream = [[NSInputStream alloc] initWithData:data];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:uploadUrl]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%ld", [data length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.inputStream = imageStream;

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    completionBlock(responseObject, nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    completionBlock(operation.responseObject,error);
}];

[operation start];

Still failed to upload, please help me solve it

It is possible to upload on the Android side. The interface for uploading images using Charles to capture packets on Android is as follows:

The results of the packet capture on iOS are as follows. After trying many methods, the server prompts that the upload failed

In addition, the server told me that this is how he obtained the input stream, but the input stream he obtained for me was nil

InputStream is = request.getInputStream();
            
            DataInputStream input = new DataInputStream(is); 
            byte[] buf = new byte[2048];
            byte[] tb = new byte[input.readInt()];


怪我咯怪我咯2718 days ago863

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-06-06 09:54:38

    application/octet-stream

    reply
    0
  • Cancelreply