gpt4 book ai didi

objective-c - 如何将文件上传到iOS上我的documents文件夹中存储的服务器?

转载 作者:行者123 更新时间:2023-12-01 16:56:52 26 4
gpt4 key购买 nike

我想上传存储在我的应用程序文档文件夹中的文件(不同格式)。在选择要在该行中上传文件的行时,我在tableview中列出了所有文件。
这是我的代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *urlString;
if (indexPath.section == 0)
{
fileUrlToUpload = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:documents[indexPath.row] ofType:nil]];
}
else
{
fileUrlToUpload = [self.documentURLs objectAtIndex:indexPath.row];

}
[self setupDocumentControllerWithURL:fileUrlToUpload];

urlString = [[fileUrlToUpload path] lastPathComponent];
printf("\n the string is :%s",[urlString UTF8String]);
[self publishToServer:urlString];

-(void)publishToServer:(NSString *)str
{
if([str length] > 0)
{

NSString *urlPath=@"";
urlPath= str;
NSString *urlString = @"http://162.108.0.8:8080/Path/upload_path.jsp?";
NSMutableURLRequest *request =[[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *postBody = [NSMutableData data];


[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"path\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:urlPath] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--",boundary] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];

[request setHTTPBody:postBody];
NSLog(@"request === %@",request);

NSData *returnData =nil;
returnData= [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSLog(@"returnData === %@",returnData);

NSString * string=[[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
}

1)当我尝试从模拟器发布文件时,服务器数据库中存储了一个空值,并且我收到“成功上传”作为响应。
2)当我试图从设备发布文件时,我在服务器中甚至没有得到null值。

请帮我。

谢谢

最佳答案

不确定文件类型,但是我可以上传图像,顺便说一句,我已经在这里尝试了所有可能的示例,但是没有用(我的$ _FILES数组为空),所以我不得不根据示例构造自己的图像,并且可以正常工作:

- (void) uploadLog :(NSString *) filePath
{
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
NSString *urlString =[NSString stringWithFormat:@"http://www.yoursite.com/accept.php"];
// to use please use your real website link.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"_187934598797439873422234";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
[request setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forHTTPHeaderField:@"Accept"];
[request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/536.26.14 (KHTML, like Gecko) Version/6.0.1 Safari/536.26.14" forHTTPHeaderField:@"User-Agent"];
[request setValue:@"http://google.com" forHTTPHeaderField:@"Origin"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"Content-Length %d\r\n\r\n", [data length] ] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"picture\"; filename=\"%@.png\"\r\n", @"newfile"] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[NSData dataWithData:data]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];


[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"];


NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
}

这是对我有用的php部分(对不起,我对jsp不熟悉):
<?php
$target_path = "./uploads/";

$target_path = $target_path . basename( $_FILES['picture']['name']);

if(move_uploaded_file($_FILES['picture']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['picture']['name'])." has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>

关于objective-c - 如何将文件上传到iOS上我的documents文件夹中存储的服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10952034/

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