gpt4 book ai didi

ios - AFNetworking 3.x 多部分表单上传

转载 作者:可可西里 更新时间:2023-11-01 04:09:37 25 4
gpt4 key购买 nike

我有一个这样的上传表单:

<form action="http://localhost/upload.php" method="post" enctype="multipart/form-data">
<input type="file" id="upload" name="upload" />
</form>

和继续上传表单的 php 代码:

isset($_FILES["upload"]) or die("Error");
// Path prepare stuff
if (move_uploaded_file($_FILES["upload"]["tmp_name"], $outputFile)) {
// Other processing stuffs
}

在 xcode 中,我是这样构造请求的:

NSMutableURLRequest* request = [[AFHTTPRequestSerializer serializer]
multipartFormRequestWithMethod:@"POST"
URLString:@"http://localhost/upload.php"
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFormData:data name:@"somefilename.ext"];
} error:nil];

但是我好像做错了,对吧?

更新

我是 AFNetworking 的新手,我想了解它如何像上面那样构建 multiplat/form-data post。看起来代码缺少输入的名称“上传”,因此将无法通过 php 上传脚本的第一行。我从 AFNetworking 的 GitHub 上阅读了文档,但他们没有提到使用 NSData 构造表单数据,这里就是这种情况。

最佳答案

好吧,在AFNetworking 3.0中,您可以像这样上传多形式零件数据,Check this

AFNetworking 3.0 is the latest major release of AFNetworking,3.0 removes all support for the now deprecated NSURLConnection based APIs. If your project was previously using these APIs, it is recommended that you now upgrade to the NSURLSession based APIs. This guide will step you through that process.

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://localhost/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

[formData appendPartWithFileData:data name:@"uploadFile" fileName:@"somefilename.txt" mimeType:@"text/plain"] // you file to upload

} error:nil];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
dispatch_async(dispatch_get_main_queue(), ^{
//Update the progress view
[progressView setProgress:uploadProgress.fractionCompleted];
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];

[uploadTask resume];

关于ios - AFNetworking 3.x 多部分表单上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34938242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com