将图像文件上载到Web表单 [英] To upload the image file to a web form

查看:103
本文介绍了将图像文件上载到Web表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文档目录(iPhone)中有一些图像文件(.png文件)。我在UIWebView上查看Web表单(.aspx)。当我点击表单的提交按钮时,我只想将图像文件附加到表单,并希望将该文件与Web表单一起发送到Web服务器。

I do have some image files(.png files) in document directory(iPhone). I am viewing web forms(.aspx) on the UIWebView. when I click on the submit button of the form, i just want to attach the image file to the form and wants to send that file to the web server along with the web form.

我遇到的是,我没有得到如何将图像文件附加到网络表单以及如何提交这两个文件。

The thing I m suffering from is, i am not getting how to attach an image file to the web form and how to submit those two.

请帮助我出于此。我没有得到任何提示怎么做。

Kindly help me out of this. I am not getting any of the hint how to do it.

感谢你....

推荐答案

您将需要修改这些示例以适合您,但它们是有效的示例。

You will need to modify these examples to work for you, but they are working examples.

MyViewController.m:

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



upload.php:

PHP

upload.php:

<?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'] ;
}
?>

这篇关于将图像文件上载到Web表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆