English 中文(简体)
cakephp 网络服务 如何写入允许我上传文件的动作
原标题:cakephp web services how to write an action that allows me to upload a file

我用的是cakephp 2.1.0。

我有一个Post C主计长,它基本上 创建了一个Post,它有 ID的整数,标题,图像像像字符串

我有一个控制器动作, 它可以用一个视图来工作, 允许我上传一个文件, 创建一个新的 Post 记录。 该动作被称为管理员_ add

这是有效的。

然而,我想揭发这个管理器_ add 的动作, 这样Adobe Flex 所建的桌面应用程序就可以把它称为 。

最好让我好好地干点活儿

基本上,我想创建这个行动 作为一个网络服务。

我看到的多数在线教程 倾向于只用于 READ 的动作, 如查看和索引 。

我要在 cakephp 应用程序代码中添加什么更改?

最佳回答

过了一段时间我就知道了

假设下列设置

  • Cakephp 2.x
  • the action here is public for anonymous users

Step 1. 安装>>Webservice Plugin ,由Josegonzalez编写。

步骤1.1. 设置路由器:

第1.2步. 增加网络服务.Web服务

1.3. 装入插件


step 2. 您需要更改“ PostC主计长” 的以下操作 < / manger > :

    public function add() { 
    
    if ($this->request->is( post )) {

        // create new Post -- this will grab the file from the request data
        $newPost = $this->Post->createNew($this->request->data);
    
        if ($newPost) {
            $this->Session->setFlash(__( Your Post has been saved ));
            // for normal webpage submission
            if (empty($this->request->params[ ext ])) {
                $this->redirect( / );                   
            } else {        
                // for json response to Flex client 
                $result = $newPost;
                $error = null;
                $id = null;
            }
        } else {
            $this->Session->setFlash(__( Your Post could not be saved. Please, try again. ));
            // for json response for failure to create
            if (!empty($this->request->params[ ext ])) {
            
                    
                    $result = null;
                    $error =  Your Post could not be saved. ;
                    $id = null;
            
            }
            
        }
        // this is for json response via Webservice.Webservice
        $this->set(compact( result ,  error ,  id ));
        
    }

    }

Step 3. 设置您在回答 < a href=> 中所说的您Flex 代码 。 >>>这里 。这是 < a href=> https://stackoverflow.com/a/10914689/80353>,然后您在 Flex Actionscript 中检索 JSON 回应 。


Step 4. 您应该期望得到由 3 变量结果、 错误、 id 和蛋糕校验错误组成的json 响应。 您可以选择按插件中指定的方式将校验错误列入黑名单 。

问题回答

听起来你的Cakephp 应用程序或多或少是合适的设置, 但后来会发生。 你现在的挑战是如何将您的 Adobe Flex 代码 拿到POST 所需的数据到 Cakephp 动作 url 。 我看到您正在使用一个行政动作, 所以我假设需要用户登录才能访问该动作 。 我知道 MINISCULE 弹性代码, 但是您需要做的是这样的 :

  1. 让 Flex POPST 到您的 /用户/ login 页面, 使用用户名/ pw 组合, 该组合具有管理员访问权限, 并将响应 cookie 和会话信息保存到变量中。 使用类似 :

    var request:URLRequest = new URLRequest(url);
    request.method = "POST";
    request.data = variables; //variables includes username and pw
    try {           
        navigateToURL(request);
    }
    
    catch (e:Error) {
    // handle error here
    }
    
  2. 然后将Flex POPST(包括请求信头中的 cookie) 以图像数据作为请求的主体(内容) 来显示您的管理/ 职务/ 动作 。

然后,在 cakephp 动作中,只需要将 $this- gt; data [body] 保存到文件中。

您可以在您的网站上制作一个正常的表格, 上传文本和图像等 。

然后,只要你想上传图像, 你就会发送正确的 POST 信息 。

例如,如果您想要使用来自其它服务器的服务,您可以使用 CURL 使用这样的代码发送窗体数据:

$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array( file  =>  @/folder/image.jpg ));
curl_setopt($ch, CURLOPT_URL,  http://website.com/posts/add );
curl_exec($ch);
curl_close($ch);

我不知道Flex是否支持CURL 但它应该有类似的东西





相关问题
IIS 6.0 hangs when serving a web-service

I am having issues with one of our web-services. It works fine on my development machine (win XP) whether I host it as a separate application or using cassini from Visual studio. Once I deploy on the ...

ASP.net web services

I am using a web service which sets the Thread.CurrentPrincipal object while logging in and soon later when another webmethod of the same web service accesses Thread.CurrentPrincipal, its different/...

Unity Container Disposing and XML Web Service

I am registering some wrapers over un-managed objects in container. How can I dispose of them at the end of the container s lifetime? Please bear in mind I have an XML Web service.

SharePoint : web service permission error

I have a sharepoint site, and I am calling a standard sharepoint web service. I create the web service request like this : wsDws.Url = this.SiteAddress + @"/_vti_bin/Dws.asmx"; When I use ...

热门标签