如果想同时post字符串以及文件到服务器,则在网页中一定要设置 enctype="multipart/form-data".
html测试页面:
<html> <body> <form action="http://www.test_letv.com/file.php" method="post"enctype="multipart/form-data"> <input type=text name="name" /> <input type=file name="file1" /> <input type="submit" /> </form> </body> </html>
|
此时,使用Firefox的Tamper Data附加组件,是看不到上传文件的过程的,只能看到post信息里面的文件名称。
而,在ASI中,这个multipart部分应该是自己给添加上的。
iPhone代码:
NSString *fileName = [[NSBundle mainBundle] pathForResource: @"01"ofType: @"mov"]; NSString *url = @"http://www.xxx.com/file.php"; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: [NSURL URLWithString: url]]; [request setPostValue: @"MyName" forKey: @"name"]; [request setFile: fileName forKey: @"this_is_file"]; [request buildRequestHeaders]; NSLog(@"header: %@", request.requestHeaders); [request startSynchronous]; NSLog(@"responseString = %@", request.responseString);
|
服务器php代码:
<?php var_dump($_FILES); $header = print_r(apache_request_headers(), true); echo $header;
|
服务器返回Log:
2011-08-07 18:33:36.991 TestRecordVideo[2836:15503] responseString = array(1) { ["this_is_file"]=> array(5) { ["name"]=> string(6) "01.mov" ["type"]=> string(15) "video/quicktime" ["tmp_name"]=> string(14) "/tmp/phpmoUHlk" ["error"]=> int(0) ["size"]=> int(942098) } } Array ( [Host] => www.xxx.com [User-Agent] => TestRecordVideo 1.0 (iPhone Simulator; iPhone OS 5.0; en_US) [Content-Length] => 942319 [Content-Type] => multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY [Accept-Encoding] => gzip [Connection] => keep-alive ) |