菜鸟教程小白 发表于 2022-12-12 14:23:46

ios - 如何在发布数据iOS objective-c 中将图像附件发送到服务器


                                            <p><p>在 postman 中向服务器发送数据,但在 Objective-C 中出现错误。</p>

<p> <img src="/image/9oFnn.png" alt="image1"/> </p>

<p>我试图从服务器上实现这个失败。我在下面提到的链接不起作用。获取错误上传失败。我做错了什么?</p>

<p> <a href="https://stackoverflow.com/questions/33074429/upload-image-to-server-ios" rel="noreferrer noopener nofollow">Upload image to server ios</a> </p>

<p> <a href="https://stackoverflow.com/questions/42487426/how-to-post-value-while-uploading-image-in-ios-objective-c" rel="noreferrer noopener nofollow">how to POST value while uploading image in iOS objective c</a> </p>

<p>我的代码:</p>

<pre><code>NSData *dataImage = UIImageJPEGRepresentation(myImage, 1.0f);

// set your URL Where to Upload Image
NSString *urlString = @&#34;http://xxxxxxxxxxxxxxxx/index.php/API/uploadClaim&#34;;

NSDictionary *user = [ objectForKey:@&#34;userDetails&#34;];

// Create &#39;POST&#39; MutableRequest with Data and Other Image Attachment.

NSMutableDictionary* _params = [ init];
;
;
objectForKey:@&#34;id&#34;]forKey:@&#34;user_id&#34;];

NSString *BoundaryConstant = @&#34;----------V2ymHFg03ehbqgZCaKO6jy&#34;;
NSString* FileParamConstant = @&#34;image&#34;;

NSURL* requestURL = ;

// create request
NSMutableURLRequest *request = [ init];
;
;
;
;

NSString *contentType = ;
;

// post body
NSMutableData *body = ;

// add params (all params are strings)
for (NSString *param in _params)
{
    dataUsingEncoding:NSUTF8StringEncoding]];
    dataUsingEncoding:NSUTF8StringEncoding]];
    ] dataUsingEncoding:NSUTF8StringEncoding]];
}

// add image data
if (dataImage) {
    dataUsingEncoding:NSUTF8StringEncoding]];
    dataUsingEncoding:NSUTF8StringEncoding]];
    ];
    ;
    dataUsingEncoding:NSUTF8StringEncoding]];
}

dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the reqeust
;

// set the content-length
NSString *postLength = ];
;
// set URL
;

NSURLResponse *response = nil;
NSError *requestError = nil;

NSData *returnData = ;
NSDictionary* json = ;
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这是我上传一个或多个图像文件的工作代码:</p>

<p>请注意:您应该在具有正确键值的 NSDictionary 中传递图像/文件数据,并且您应该将键名称设置为您的服务器在上传调用中所期望的。<br/>
例如,在您的情况下,它是 <code>file</code> </p>

<p><code>parameters</code> 字典将是您与 <code>file</code></p> 一起发送的其他参数

<pre><code>- (void) performNetworkEventRequestCallWithFileUpload :(NSMutableDictionary*) imagesData : (NSMutableDictionary*)parameters completionBlock:(void (^)(BOOL succeeded, NSDictionary *dict))completionBlock {


NSMutableURLRequest *urlRequest = [ init];
];
;
NSString *boundary = @&#34;---------------------------14737809831466499882746641449&#34;;
NSString *contentType = ;
;
NSMutableData *body = ;


[parameters enumerateKeysAndObjectsUsingBlock: ^(NSString *key, NSString *object, BOOL *stop) {
    dataUsingEncoding:NSUTF8StringEncoding]];
    dataUsingEncoding:NSUTF8StringEncoding]];
    dataUsingEncoding:NSUTF8StringEncoding]];
}];


[imagesData enumerateKeysAndObjectsUsingBlock: ^(NSString *key, NSData *object, BOOL *stop) {
    if (]) {
      if (object.length &gt; 0) {
             dataUsingEncoding:NSUTF8StringEncoding]];
            NSString *timeSTamp = ];
             dataUsingEncoding:NSUTF8StringEncoding]];
             dataUsingEncoding:NSUTF8StringEncoding]];
            ];
      }

    }
}];


dataUsingEncoding:NSUTF8StringEncoding]];
;


AFHTTPSessionManager* manager = ;
manager.responseSerializer.acceptableContentTypes = nil;

NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:urlRequest completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
      NSLog(@&#34;Error: %@&#34;, error);
      completionBlock (NO, responseObject);
    } else {
      NSLog(@&#34;success&#34;);
      completionBlock (YES, responseObject);
    }
}];
;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在发布数据iOS objective-c 中将图像附件发送到服务器,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47874414/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47874414/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在发布数据iOS objective-c 中将图像附件发送到服务器