dsavz66262 2013-06-19 15:54
浏览 52
已采纳

使用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 2013-06-20 10:14
    关注

    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...

    :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!