简体   繁体   English

使用cURL将文件上传到asp.net mvc操作

[英]Upload File using cURL to asp.net mvc action

I am trying to upload a file from PHP using cURL to a asp.net mvc 4 controller action. 我正在尝试使用cURL从PHP上传文件到asp.net mvc 4控制器操作。 The file is successfully uploaded on disk after move_uploaded_file is called. 调用move_uploaded_file后,文件已成功上传到磁盘上。

Both .NET MVC and PHP sites are hosted under IIS. .NET MVC和PHP站点都托管在IIS下。 I get this HTTP Response: 我收到此HTTP响应:

Bad Request - Invalid Content Length 错误的请求-无效的内容长度

Has anyone achieved this? 有人做到了吗?

Relevant PHP code: 相关的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); 
    }

Relevant .NET C# code: 相关的.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
        ...
     }

Are you sure about $chpg? 您确定$ chpg吗? i believe this is where are going wrong, perhaps you need to change $chpg to $ch, try using the following 2 lines of code: 我认为这是哪里出了问题,也许您需要将$ chpg更改为$ ch,请尝试使用以下两行代码:

curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

Instead of: 代替:

curl_setopt($chpg, CURLOPT_POST, count($data));
curl_setopt($chpg, CURLOPT_POSTFIELDS, $data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM