xcode 使用 iPhone sdk 将多格式数据发送到服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10051150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiform data send to server using iPhone sdk
提问by Cherr Skees
I've been trying to follow some examples for uploading data to a website. I want to save the message as a .txt file. Any ideas what to do here?
我一直在尝试遵循一些将数据上传到网站的示例。我想将消息保存为 .txt 文件。任何想法在这里做什么?
- (IBAction)sendSerializedGreeting:(id)sender;
{
NSString *message;
message =@"Here we go for a test message.";
// Show a loading indicator
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString *greetingURL = [NSString stringWithFormat:@"http://www.site.com/upload.php"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:greetingURL]
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSDictionary *headerFieldsDict = [NSDictionary dictionaryWithObjectsAndKeys:@"text/xml; charset=utf-8", @"Content-Type", nil];
[theRequest setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]];
[theRequest setAllHTTPHeaderFields:headerFieldsDict];
[theRequest setHTTPMethod:@"POST"];
// create the connection with the request and start loading the data
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection == nil)
{
NSLog(@"Failed to create the connection");
}
}
Here is the upload.php file. I'm assuming the issue is with this but not sure. I don't know where the filename gets set in the example above. This code was used from somewhere else.
这是upload.php 文件。我假设问题与此有关,但不确定。我不知道上面示例中文件名的设置位置。此代码是从其他地方使用的。
<?php
$uploaddir = './'; //Uploading to same directory as PHP file
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
$randomNumber = rand(0, 99999);
$newName = $uploadDir . $randomNumber . $uploadFile;
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
echo "Temp file uploaded.";
} else {
echo "Temp file not uploaded.";
}
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $newName)) {
}
?>
回答by dayitv89
I am sending a image to server through this code .... use for your text file or any type of multiform data
我正在通过此代码向服务器发送图像......用于您的文本文件或任何类型的多格式数据
NSString *strUrl=@"http://www.site.com/upload.php";
NSLog(@"%@",strUrl);
NSURL *url=[NSURL URLWithString:strUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:130.0];
// file name of user picture
// I am sending a image to server
NSString *fileName=[[eMailTxtField.text stringByReplacingOccurrencesOfString:@"@" withString:@"-"] stringByAppendingString:@".jpg"];
//image data
UIImage *userImage=imgUser.image;
NSData *imageData =UIImageJPEGRepresentation(userImage, 90);
NSMutableData *myRequestData = [[NSMutableData alloc] init];
[myRequestData appendData:[NSData dataWithBytes:[postData UTF8String] length:[postData length]]];
NSString *boundary = [NSString stringWithString:@"--"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
[myRequestData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadfile\"; filename=\"%@\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[NSData dataWithData:imageData]];
[myRequestData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[ request setHTTPMethod: @"POST" ];
[ request setHTTPBody: myRequestData ];
NSURLResponse *response;
NSError *error;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&error];
if (!error && returnData) {
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
[self registerWithJsonString:content];
}
else{
[self showServerNotFoundError];
}