試圖理解下面的代碼及其簡單的HTTP POST方法(在本例中後期圖像數據在這裏)發送數據到服務器...
- (id)initWithURL:(NSString *)url requestDataForImage:(NSData *)dataForImage imagename:(NSString*)imagename
{
if (self = [super init])
{
[email protected]"Sourceimagetype";
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body1 = [NSMutableData data];
[body1 appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//int r = arc4random() % 480000;
NSString *str = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"SourceImage\"; filename=\"%@\"\r\n", imagename];
[body1 appendData:[[NSString stringWithString:str] dataUsingEncoding:NSUTF8StringEncoding]];
[body1 appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding : NSUTF8StringEncoding]];
[body1 appendData:[NSData dataWithData:dataForImage]];
[body1 appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body1];
con=[[NSURLConnection alloc]initWithRequest:request delegate:self];
if(con)
{
webdata=[[NSMutableData data]retain];
}
}
return self;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webdata setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webdata appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[app hideLoadingView];
if ([WSDelegate respondsToSelector:@selector(ConnectionFail:)])
{
[WSDelegate ConnectionFail:[NSString stringWithFormat:@"%@",error.localizedDescription]];
}
else
{
connection=nil;
[connection release];
}
}
在服務器上的PHP文件
您可以我們的文件移動到所需的文件夾..
if ($_GET['user_id']!='')
{
$uploaddir = $_SERVER['DOCUMENT_ROOT'].'/streetculture/Images/'.$user_id.'/';
move_uploaded_file($_FILES['SourceImage']['tmp_name'],$uploaddir.$_FILES['SourceImage']['name']);
$da_created = date('Y-m-d H:i:s');
@$Carpic = $_FILES['SourceImage']['name'];
$thumbname='th_'.$Carpic;
$uploadfile = $uploaddir . $Carpic;
$tx_image = $Carpic;
list($width, $height) = getimagesize($uploadfile) ;
$ratio=2;
$modwidth = $width/$ratio;
$modheight = $height/$ratio;
$modwidth=round($modwidth, 0, PHP_ROUND_HALF_EVEN);
$modheight=round($modheight, 0, PHP_ROUND_HALF_EVEN);
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($uploadfile) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
imagejpeg($tn,$uploaddir . $thumbname, 100) ;
}
您是否正在製作自己的服務器? – cahn
解釋更多deatail問題,無論你想編碼的幫助或其他?如果你想要代碼的幫助,然後給正確的URL和其他參數傳遞。 –