dsavz66262
dsavz66262
2013-06-19 15:54

使用PHP / CURL将文件发布到ASP.NET MVC 2控制器

已采纳

sorry if this might be a cheesy question, but unfortunately I cannot change the project setup. I have a form on a php page, which is supposed to post some text fields and a file input to a MVC Controller Action. The posting part works, and I successfully receive the text data in my ViewModel. However, I have no idea how tto receive the file (image, will be either jpg or png). The ID and NAME of the input type file is "UserIcon".

In the ActionResult I tried it with HttpPostedFileBase like this:

public virtual ActionResult RegisterJson(RegistrationModels model, HttpPostedFileBase UserIcon)

But UserIcon is always NULL. Instead, if I have the file as part of the RegistrationModels Model:

    [DisplayName("Ihr Foto")]
    public string UserIcon { get; set; }   

I receive the curl path: @C:\\Windows\\Temp\\phpA03D.tmp;filename=Penguins.jpg; as string.

But from here I have absolutely no clue as to what I have to do next to save the file. I tried to add to the curl header the $header = array("Content-type: multipart/form-data"); but then all the posted form fields are NULL.

I'd be so grateful if anyone could point me in the right direction! I think I'm having a main blackout here and doing something completely wrong :-S

This is the curl code which posts to the MVC Action:

    $postargs = array();
    foreach ($_FILES as $param => $file) {
        $postargs[$param] = '@' . $file['tmp_name'] . ';filename=' . $file['name'] . ';';
    }
    $fields_string = "";
    //url-ify the data for the POST
    foreach($_POST as $key=>$value) {
        $fields_string .= $key.'='.$value.'&';
    }

    $fields_string .= http_build_query($postargs);

    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,count($_POST));
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

    $result = curl_exec($ch);

    curl_close($ch);

Thanks alot!!

Cheers, Caroline

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • duan19913 duan19913 8年前

    Sometimes it helps just sleeping over it. One small sentence helped me out: you have to save the file on the server before posting it with curl. Right!! For anyone who might be interested, I'm posting my working solution:

    $upload_directory=str_replace("\includes","",dirname(__FILE__)).'/local/';
    
    //check if form submitted
    
    if (!empty($_FILES['UserPortrait'])) {
        //check for image submitted
        if ($_FILES['UserPortrait']['error'] > 0) {
            // check for error re file
            echo "Error: " . $_FILES["UserPortrait"]["error"] ;
            exit;
        } else {
            //move temp file to our server
            move_uploaded_file($_FILES['UserPortrait']['tmp_name'],
            $upload_directory . $_FILES['UserPortrait']['name']);
            //echo 'Uploaded File.';
        }
    } else {
        die('File not uploaded.');
        // exit script
    }
    
    
    $post_data = array (  
        "UserPortrait" => "@".$upload_directory . $_FILES['UserPortrait']['name'].';type='.$_FILES['UserPortrait']['type']    
    ); 
    foreach($_POST as $key=>$value) {
        $post_data[$key] = $value;
    }
    
    $ch = curl_init();      
    curl_setopt($ch, CURLOPT_URL, "http://www.example.com/handler.php");      
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
    curl_setopt($ch, CURLOPT_POST, 1);      
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);     
    $output = curl_exec($ch);      
    curl_close($ch);      
    echo $output;  
    

    Receiving the post data in my MVC Controller action works like a charm now:

    public virtual ActionResult RegisterJson(RegisterModels model, HttpPostedFileBase UserPortrait)
    

    ...and from there do the rest...

    :-)

    点赞 评论 复制链接分享