2016-09-27 118 views
0

我是symfony REST fromework的新手。在我们的项目中,我们使用ng-upload来上传图像。下面是对此负责的一段代码。ng-flow抛出404错误

// HTML

<div style="float: left; margin-top: 7px; width: 220px;" permission-roles="['ROLE_UNIT_WRITE']" flow-init="{target: getFlowJsTarget(), testChunks:false, headers: getFlowJsHeaders()}" flow-file-added="!!{jpg:1,jpeg:1}[$file.getExtension()]" flow-files-submitted="$flow.upload()" flow-file-success="$file.msg = $message"></div> 

//在控制器(前端)

$scope.getFlowJsTarget = function() { 
    return Routing.generate('api_1_post_unit_photo', {id: $scope.unit.id}); 
}; 

$scope.getFlowJsHeaders = function() { 
    return {'X-XSRF-TOKEN': $cookies['XSRF-TOKEN']}; 
}; 

$scope.getUnitPhotoUrl = function(unit) { 
    return Routing.generate('api_1_get_unit_photo', {id: unit.id}); 
}; 

//后端代码 - UnitController.php

public function getUnitPhotoAction(Request $request, $id) 
{ 
    $unit = $this->getOr404($id); 

    $response = new Response(); 

    if ($unit && $unit->getPhoto()) { 
     $response->headers->set('Cache-Control', 'private'); 
     $response->headers->set('Content-type', 'image/jpeg'); 
     $response->headers->set('X-Accel-Redirect', '/unit-photos/'.$unit->getPhoto()); 

    } else { 
     $response->setStatusCode(404); 
    } 

    return $response; 
} 

public function getUnitPhotosAction(Request $request, $id) 
{ 
    $this->getOr404($id); 

    $flowJsHelper = $this->get('isf.flow_js_helper'); 

    if ($flowJsHelper->checkChunk()) { 
     return new JsonResponse(null, 200); 
    } else { 
     return new JsonResponse(null, 404); 
    } 
} 
public function postUnitPhotoAction(Request $request, $id) 
{ 
    $unit = $this->getOr404($id); 

    $flowJsHelper = $this->get('isf.flow_js_helper'); 
    $flowJsHelper->saveChunk(); 

    $data = [ 
     'success' => true, 
    ]; 

    $unitPhotoPath = $this->container->getParameter('kernel.root_dir').'/../web/uploads/unit-photos'; 
    $fileName = $unit->getId().'.jpg'; 

    if ($flowJsHelper->validateFile() && $flowJsHelper->save($unitPhotoPath.'/'.$fileName)) { 
     $data['finished'] = true; 
     $unit->setPhoto($fileName); 
     $this->getHandler()->persist($unit, false); 
    } 

    return new JsonResponse($data); 
} 

//FlowJSHelper.php

public function checkChunk() 
{ 
    return $this->filesystem->exists($this->getChunkPath($this->getParameter('flowChunkNumber'))); 
} 

public function saveChunk() 
{ 
    /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */ 
    $file = $this->getRequest()->files->get('file'); 
    $chunkFile = $this->getChunkPath(); 
    $file->move(dirname($chunkFile), basename($chunkFile)); 
} 

public function getChunkPath($index = null) 
{ 
    if (null === $index) { 
     $index = $this->getParameter('flowChunkNumber'); 
    } 

    return $this->tempDir . DIRECTORY_SEPARATOR . $this->getParameter('flowIdentifier') . '_' . $index; 
} 

错误我变得像this

NG-流standalone.js:1249 GET http://localhost:30080/api/v1/units/5732fa2dec045be8448b457d/photos?flowChu ... st1jpg & flowFilename = test1.jpg & flowRelativePath = test1.jpg & flowTotalChunks = 1 404(未找到)

+0

我不确定我是否忽略了某些内容,但不是'Routing.generate'是您在js客户端应用程序中使用的Symfony函数?但是你可能使用你自己的前端实现,我会假设它是正确的。 – kvetis

+0

是的。你是对的。 – Pradeepb

+0

现在好了我挖了更多,它是Symfony JS捆绑,thx澄清。 – kvetis

回答

1

它看起来像你正在发送和GET请求。当您尝试上载图像时,是否应该发送POST请求?

您的服务器是否期望GET或POST请求?

尝试用其他配置

flow-init="{testChunks:false}" 

一起加入testChunks:false使NG流不发送GET请求。

+0

我去槽文件[这里](https://github.com/flowjs/ng-flow)。我相信'$ flow.upload()'会做到这一点。纠正我,如果我错了。 – Pradeepb

+0

所以你的服务器期待POST请求? –

+0

你可以尝试在flow-init中将testChuncks配置设置为false吗? –