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

    :-)

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

报告相同问题?

悬赏问题

  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗