ios - NSURLSessionUploadTask 不上传带参数的图片
<p><p>我有以下代码,它将图像和一些文本发送到我的服务器:</p>
<pre><code> NSURLSessionConfiguration *defaultConfigObject = ;
self.session = ;
NSString *requestURL = @"http://www.website.com.br/receive.php?name=StackOverflow";
NSMutableURLRequest *request = ];
;
UIImage *imagem = ;
NSData *imageData = UIImageJPEGRepresentation(imagem, 1.0);
self.uploadTask = ;
;
-(void)URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{
NSString* newStr = [ initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",newStr);
}
</code></pre>
<p><strong>PHP</strong></p>
<pre><code><?php
echo $_POST['name'];
?>
</code></pre>
<p>这段代码的问题是 <code>didReceiveData</code> 方法没有接收到服务器的数据,当我把这段代码放在 php 文件中时它只得到一个 <code>NSData</code> :</p>
<pre><code>print_r($_FILES);
</code></pre>
<p>然而它返回一个空数组,为什么会这样?</p>
<p><strong>已解决</strong></p>
<p>好吧,我解决了我的问题,让我们开始吧,在 .h 文件中你需要实现这个协议(protocol)和一个属性:</p>
<pre><code>< NSURLSessionDelegate, NSURLSessionTaskDelegate>
@property (nonatomic) NSURLSessionUploadTask *uploadTask;
</code></pre>
<p>而在 .m 文件中有一个 IBAction 类型的方法并且它连接到我们 View 中存在的特定按钮,我们只需要这样做:</p>
<pre><code>- (IBAction)start:(id)sender {
if (self.uploadTask) {
NSLog(@"Wait for this process finish!");
return;
}
NSString *imagepath = [.path stringByAppendingPathComponent:@"myImage.jpg"];
NSURL *outputFileURL = ;
// Define the Paths
NSURL *icyURL = ;
// Create the Request
NSMutableURLRequest* request = ;
;
// Configure the NSURL Session
NSURLSessionConfiguration *sessionConfig = ;
NSURLSession *upLoadSession = ;
// Define the Upload task
self.uploadTask = ;
// Run it!
;
}
</code></pre>
<p>并实现一些委托(delegate)方法:</p>
<pre><code>- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
NSLog(@"didSendBodyData: %lld, totalBytesSent: %lld, totalBytesExpectedToSend: %lld", bytesSent, totalBytesSent, totalBytesExpectedToSend);
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
if (error == nil) {
NSLog(@"Task: %@ upload complete", task);
} else {
NSLog(@"Task: %@ upload with error: %@", task, );
}
}
</code></pre>
<p>最后,您需要使用以下代码创建一个 PHP 文件:</p>
<pre><code><?php
$fp = fopen("myImage.jpg", "a");//If image come is .png put myImage.png, is the file come is .mp4 put myImage.mp4, if .pdf myImage.pdf, if .json myImage.json ...
$run = fwrite($fp, file_get_contents("php://input"));
fclose($fp);
?>
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>上传图片到Dropbox的代码示例。</p>
<pre><code>// 1. config
NSURLSessionConfiguration *config = ;
// 2. if necessary set your Authorization HTTP (example api)
// ;
// 3. Finally, you create the NSURLSession using the above configuration.
NSURLSession *session = ;
// 4. Set your Request URL (example using dropbox api)
NSURL *url = ;;
NSMutableURLRequest *request = [ initWithURL:url];
// 5. Set your HTTPMethod POST or PUT
;
// 6. Encapsulate your file (supposse an image)
UIImage *image = ;
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
// 7. You could try use uploadTaskWithRequest fromData
NSURLSessionUploadTask *taskUpload = [session uploadTaskWithRequest:request fromData:imageData completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
if (!error && httpResp.statusCode == 200) {
// Uploaded
} else {
// alert for error saving / updating note
NSLog(@"ERROR: %@ AND HTTPREST ERROR : %ld", error, (long)httpResp.statusCode);
}
}];
- (NSURL*)uploadURLForPath:(NSString*)path
{
NSString *urlWithParams = [NSString stringWithFormat:@"https://api-content.dropbox.com/1/files_put/sandbox/%@/%@",
appFolder,
];
NSURL *url = ;
return url;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - NSURLSessionUploadTask 不上传带参数的图片,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/28570574/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/28570574/
</a>
</p>
页:
[1]