⑴ python如何模拟含有文件上传的表单
在机器上安装了 Python 的
setuptools,可以通过下面的命令来安装 poster:
easy_installposter
装完之后,就可以像用下面代码模拟上传文件表单了:
fromposter.encodeimportmultipart_encode
fromposter.streaminghttpimportregister_openers
importurllib2
#在urllib2上注册http流处理句柄
register_openers()
#开始对文件"DSC0001.jpg"的multiart/form-data编码
#"image1"是参数的名字,一般通过HTML中的<input>标签的name参数设置
#headers包含必须的Content-Type和Content-Length
#datagen是一个生成器对象,返回编码过后的参数
datagen,headers=multipart_encode({"image1":open("DSC0001.jpg","rb")})
#创建请求对象
request=urllib2.Request("http://localhost:5000/upload_image",datagen,headers)
#实际执行请求并取得返回
printurllib2.urlopen(request).read()
⑵ 如何在 Python 中模拟 post 表单来上传文件
在机器上安装了Python的setuptools工具,可以通过下面的命令来安装 poster:
easy_installposter
装完之后,安装下面代码就可以实现post表单上传文件了:
fromposter.encodeimportmultipart_encode
fromposter.streaminghttpimportregister_openers
importurllib2
#在urllib2上注册http流处理句柄
register_openers()
#开始对文件"DSC0001.jpg"的multiart/form-data编码
#"image1"是参数的名字,一般通过HTML中的<input>标签的name参数设置
#headers包含必须的Content-Type和Content-Length
#datagen是一个生成器对象,返回编码过后的参数
datagen,headers=multipart_encode({"image1":open("DSC0001.jpg","rb")})
#创建请求对象(localhost服务器IP地址,5000服务器端口)
request=urllib2.Request("http://localhost:5000/upload_image",datagen,headers)
#实际执行请求并取得返回
printurllib2.urlopen(request).read()
⑶ 如何利用curl实现form表单提交 带文件上传
//上传D盘下的来test.jpg文件,文件必须自存在,否则curl处理失败且没有任何提示
$data=array('name'=>'Foo','file'=>'@d:/test.jpg');
注:PHP5.5.0起,文件上传建议使用CURLFile代替@
$ch=curl_init('http://localhost/upload.php');
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_exec($ch);
更多内容请参考:http://www.zjmainstay.cn/php-curl#十模拟上传文件
⑷ js 模拟POST提交enctype="multipart/form-data"类型的表单
只是需要文件上传才用它的
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
改成
xmlHttp.setRequestHeader("Content-Type","multipart/form-data;")。
⑸ 请教用Curl 在php 里面模拟表单提交 文本+文件的写法
publicfunctioncurl($url,$postFields=null)
{
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_FAILONERROR,false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
if($this->readTimeout){
curl_setopt($ch,CURLOPT_TIMEOUT,$this->readTimeout);
}
if($this->connectTimeout){
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$this->connectTimeout);
}
//https请求
if(strlen($url)>5&&strtolower(substr($url,0,5))=="https"){
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
}
if(is_array($postFields)&&0<count($postFields))
{
$postBodyString="";
$postMultipart=false;
foreach($postFieldsas$k=>$v)
{
if("@"!=substr($v,0,1))//判断是不是文件上传
{
$postBodyString.="$k=".urlencode($v)."&";
}
else//文件上传用multipart/form-data,否则用www-form-urlencoded
{
$postMultipart=true;
}
}
unset($k,$v);
curl_setopt($ch,CURLOPT_POST,true);
if($postMultipart)
{
foreach($postFieldsas$k=>$v){
if("@"==substr($v,0,1)){
$tempffile=preg_replace('/^@/','',$v);
$advfield[$k]=newCURLFile($tempffile);
}else{
$advfield[$k]=$v;
}
}
curl_setopt($ch,CURLOPT_POSTFIELDS,$advfield);
unset($k,$v,$advfield);
//curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);//田村改
//curl_setopt($ch,CURLOPT_POSTFIELDS,['file'=>newCURLFile(realpath('image.png'))]);
}
else
{
curl_setopt($ch,CURLOPT_POSTFIELDS,substr($postBodyString,0,-1));
}
}
$reponse=curl_exec($ch);
if(curl_errno($ch))
{
thrownewException(curl_error($ch),0);
}
else
{
$httpStatusCode=curl_getinfo($ch,CURLINFO_HTTP_CODE);
if(200!==$httpStatusCode)
{
thrownewException($reponse,$httpStatusCode);
}
}
curl_close($ch);
return$reponse;
}
表单列表是 $postFields 传入参数
数组,如果有文件 ,就在数组的值 前面加@
已经做好的 集成类 的实现 其他类字段和方法没给出,写不下了。
但是大致的实现过程应该可以看懂了
⑹ 模拟JQUERY多文件上传效果
<!doctypehtml>
<htmllang="zh">
<head>
<metacharset="utf-8">
<title>HTML5AjaxUploader</title>
<scriptsrc="jquery-2.1.1.min.js"></script>
</head>
<body>
<p><inputtype="file"id="upfile"></p>
<p><inputtype="button"id="upJS"value="用原生JS上传"></p>
<p><inputtype="button"id="upJQuery"value="用jQuery上传"></p>
<script>
/*原生JS版*/
document.getElementById("upJS").onclick=function(){
/*FormData是表单数据类*/
varfd=newFormData();
varajax=newXMLHttpRequest();
fd.append("upload",1);
/*把文件添加到表单里*/
fd.append("upfile",document.getElementById("upfile").files[0]);
ajax.open("post","test.php",true);
ajax.onload=function(){
console.log(ajax.responseText);
};
ajax.send(fd);
}
/*jQuery版*/
$('#upJQuery').on('click',function(){
varfd=newFormData();
fd.append("upload",1);
fd.append("upfile",$("#upfile").get(0).files[0]);
$.ajax({
url:"test.php",
type:"POST",
processData:false,
contentType:false,
data:fd,
success:function(d){
console.log(d);
}
});
});
</script>
</body>
</html>
<?php
if(isset($_POST['upload'])){
var_mp($_FILES);
move_uploaded_file($_FILES['upfile']['tmp_name'],'up_tmp/'.time().'.dat');
//header('location:test.php');
exit;
}
?>
⑺ C#代码模拟Form表单提交同时还得上传XML文件
不知道理解对不对
有个笨的方法 就是在后台生成js代码 这个js代码的功能就是生成表单 让后再让这个js代码触发form提交事件