javascript - ng-flow 抛出 404 错误

标签 javascript php angularjs symfony ng-flow

我是 symfony REST fromework 的新手。在我们的项目中,我们使用 ng-upload 来上传图像。下面是负责此操作的代码段。

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

//在 Controller (前端)

$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-flow-standalone.js:1249 GET http://localhost:30080/api/v1/units/5732fa2dec045be8448b457d/photos?flowChu…st1jpg&flowFilename=test1.jpg&flowRelativePath=test1.jpg&flowTotalChunks=1 404 (Not Found)

最佳答案

看起来您正在发送 GET 请求。当您尝试上传图像时,您不应该发送 POST 请求吗?

您的服务器是否需要 GET 或 POST 请求?

尝试添加 testChunks:false 以及其他配置

flow-init="{testChunks:false}" 

这样 ng-flow 就不会发送 GET 请求。

关于javascript - ng-flow 抛出 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39722926/

相关文章:

javascript - DIVstyle=none,但div仍然显示(用于弹出处理图形)

javascript - 如何使用 javascript 仅将目标 ="_blank"添加到某些链接?

javascript - 使用 SQLite 插入/读取 Base64 字符串

php - 授予 bjyauthorize 从 CLI 运行 ZF2 的 mvc 应用程序的权限

php - 对多行进行连接查询以进行搜索

javascript - 如何更改 AngularJS 中的一次性绑定(bind)数据?

javascript - 使用 Knockout.js 的绑定(bind)问题?

java - 如何在 Android 中使用 PHP MYSQL 更新数据

javascript - AngularJS 和 Django 的 DOM、JavaScript 和服务器端数据库之间是否有任何三向数据绑定(bind)框架?

javascript - 使用 PDF.js 进行双指缩放