Iphone 将图像文件上载到web表单的步骤
我在文档目录(iPhone)中有一些图像文件(.png文件)。我正在UIWebView上查看web表单(.aspx)。当我单击表单的submit按钮时,我只想将图像文件附加到表单,并希望将该文件与web表单一起发送到web服务器 我所遭受的痛苦是,我不知道如何将图像文件附加到web表单以及如何提交这两个文件 请帮我解决这个问题。我不知道该怎么做Iphone 将图像文件上载到web表单的步骤,iphone,objective-c,Iphone,Objective C,我在文档目录(iPhone)中有一些图像文件(.png文件)。我正在UIWebView上查看web表单(.aspx)。当我单击表单的submit按钮时,我只想将图像文件附加到表单,并希望将该文件与web表单一起发送到web服务器 我所遭受的痛苦是,我不知道如何将图像文件附加到web表单以及如何提交这两个文件 请帮我解决这个问题。我不知道该怎么做 谢谢您……您需要修改这些示例来为您工作,但它们都是工作示例 目标-C MyViewController.m: - (void) upload {
谢谢您……您需要修改这些示例来为您工作,但它们都是工作示例 目标-C
MyViewController.m:
- (void) upload
{
NSString *urlString = @"http://www.yourwebsite.com/uploads/upload.php";
NSData *data = /* turn your png into NSData */
// setting up the request object now
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"];
/* now lets create the body of the post */
NSString *content = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.png\"\r\n",@"yourPng"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:content] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: text/plain\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
}
<?php
//assuming upload.php and upload folder are in the same dir
$uploaddir = 'uploads/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
$product = $_GET[product];
if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
echo "PNG uploaded. \r\n";
} else {
echo "PNG not uploaded. \r\n";
}
if ($_FILES['userfile']['size']> 300000) //Limiting image at 300K
{
exit("Your file is too large.");
}
// Add support here for PNG files:
if ((!($_FILES['userfile']['type'] == "text/plain")) && //Also allowing
(!($_FILES['userfile']['type'] == "text/plist")) && //plist files
(!($_FILES['userfile']['type'] == "text/html"))) //HTML files
{
exit("Incorrect file type. " . $_FILES['userfile']['type'] . " is the file type you uploaded.");
}
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
$postsize = ini_get('post_max_size'); //Not necessary, I was using these
$canupload = ini_get('file_uploads'); //server variables to see what was
$tempdir = ini_get('upload_tmp_dir'); //going wrong.
$maxsize = ini_get('upload_max_filesize');
echo "http://www.yourwebsite.com/uploads/{$file}" . "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ;
}
?>
PHP
upload.php:
- (void) upload
{
NSString *urlString = @"http://www.yourwebsite.com/uploads/upload.php";
NSData *data = /* turn your png into NSData */
// setting up the request object now
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"];
/* now lets create the body of the post */
NSString *content = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.png\"\r\n",@"yourPng"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:content] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: text/plain\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
}
<?php
//assuming upload.php and upload folder are in the same dir
$uploaddir = 'uploads/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
$product = $_GET[product];
if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
echo "PNG uploaded. \r\n";
} else {
echo "PNG not uploaded. \r\n";
}
if ($_FILES['userfile']['size']> 300000) //Limiting image at 300K
{
exit("Your file is too large.");
}
// Add support here for PNG files:
if ((!($_FILES['userfile']['type'] == "text/plain")) && //Also allowing
(!($_FILES['userfile']['type'] == "text/plist")) && //plist files
(!($_FILES['userfile']['type'] == "text/html"))) //HTML files
{
exit("Incorrect file type. " . $_FILES['userfile']['type'] . " is the file type you uploaded.");
}
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
$postsize = ini_get('post_max_size'); //Not necessary, I was using these
$canupload = ini_get('file_uploads'); //server variables to see what was
$tempdir = ini_get('upload_tmp_dir'); //going wrong.
$maxsize = ini_get('upload_max_filesize');
echo "http://www.yourwebsite.com/uploads/{$file}" . "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ;
}
?>
如果您试图从iphone safari上传文件。 你完蛋了,不支持从iPhone safari上传文件
将被禁用。解决了我自己的问题!最后一条回音线需要调整
<?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'])) {
} else {
echo "Temp file not uploaded. \r\n";
}
if ($_FILES['userfile']['size']> 3000000000000000) {
exit("Your file is too large.");
}
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $newName)) {
$postsize = ini_get('post_max_size'); //Not necessary, I was using these
$canupload = ini_get('file_uploads'); //server variables to see what was
$tempdir = ini_get('upload_tmp_dir'); //going wrong.
$maxsize = ini_get('upload_max_filesize');
echo "http://localhost:8888/wouldYa/images/{$randomNumber}{$file}" . "\r\n";
}
?>
现在,我正在将Str保存在Xcode中,保存到另一个PHP文件中,将文件名插入到我表中该用户的部分,特别是显示图片部分,这应该不难实现:-)
如果您对我的完整源代码感兴趣,请告诉我,我知道它还没有那么安全,但很快就会安全。先生,您的代码正在帮助我。但是,问题是,请你用C#Net发布你已经用php发布的服务器端代码。我正在寻找用于在服务器端获取NSData的C#.net语言……对文件大小有任何限制吗?