使用NSUrlRequest上载文件会导致零字节文件大小 [英] Uploading a file using NSUrlRequest results in Zero Bytes filesize
本文介绍了使用NSUrlRequest上载文件会导致零字节文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用目标c和NSUrlRequest进行简单的文件上传。
I'm trying to do a simple file upload using objective c and NSUrlRequest.
我当前的(或多或少googled并放在一起)代码:
My current (more or less googled and put together) code:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://127.0.0.1:8000/api/upload/"]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postData = [NSMutableData data];
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Append the Usertoken
[postData appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"token\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithString:@"Content-Type: application/json\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"%@", token] dataUsingEncoding:NSUTF8StringEncoding]];
// Append the file
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"fileupload\"; filename=\"%@\"\r\n", file]dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// Close
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Append
[request setHTTPBody:postData];
所有上传的文件都有Zero Bytes filesize。服务器端部分正常工作。用cURL测试。
All uploaded files having Zero Bytes filesize. The serverside part is working correctly. Tested with cURL.
我忘记了什么吗?
推荐答案
发现我的错。这是一个非常简单的问题,因为我对http缺乏了解。
Found my mistake. Was a very simple one, happened because of my lack of knowledge about http.
我忘了在octet-stream之后附加文件数据
I forgot to append the file data after the octet-stream
NSData *photo = [[NSFileManager defaultManager] contentsAtPath:file];
// Append the file
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"fileupload\"; filename=\"%@\"\r\n", file]dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[NSData dataWithData:photo]];
这篇关于使用NSUrlRequest上载文件会导致零字节文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文