我正试图使用cURL将一个文件从PHP上传到asp.net mvc 4控制器操作。调用move_uploaded_file
后,文件成功上传到磁盘上。
.NET MVC和PHP站点都托管在IIS下。我得到这个HTTP响应:
错误的请求-无效的内容长度
有人做到了吗?
相关PHP代码:
if(move_uploaded_file($from, $to))
{
$imageFile = '@' . $to . ';type='.$_FILES['file']['type'];
$post_data = http_build_query($_POST);
$post_data["imageFile"] = $imageFile;
$ch = curl_init(self::URL . '/photos/uploadphoto');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($chpg, CURLOPT_POST, count($data));
curl_setopt($chpg, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
}
相关.NET C#代码:
[HttpPost]
public ActionResult UploadPhoto(HttpPostedFileBase imagefile)
{
// imageFile is null
var file = Request.Files.Count > 0 ? Request.Files[0] : null;
// file is also null
...
}
你确定$chpg吗?我相信这就是问题所在,也许你需要将$chpg更改为$ch,尝试使用以下两行代码:
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
代替:
curl_setopt($chpg, CURLOPT_POST, count($data));
curl_setopt($chpg, CURLOPT_POSTFIELDS, $data);