簡體   English   中英

上載文件(圖像)在實時服務器上不起作用,但在dev上起作用

[英]Uploading a file (image) doesn't work on live server but works on dev

我無法解決這個問題。 以下代碼在我的本地計算機上可用,但是將站點上傳到服務器后,我似乎沒有在請求中收到文件。

這是我的ajax:

dialogUploadPhoto.find('form').submit(function(event) {
    event.preventDefault();
    $.ajax({
        type: $(this).attr('method'),
        url: $(this).attr('action'),
        data: $(this).serializeForm(), // method below...
        // data: formData,
        processData: false,
        contentType: false,
        cache: false,
        success: function(response) {
            // The response has the correct date on my local server
            dialogUploadPhoto.dialog( "close" );
        },
        error: function (xhr, desc, err){
            // On the live server, the debugger hits
        }
    });
});

serializeForm方法只是一個將所有字段附加到FormData實例的jquery方法:

$.fn.serializeForm = function() {
    var form = $(this),
        formData = new FormData();
    var formParams = form.serializeArray();

    $.each(form.find('input[type="file"]'), function(i, tag) {
        $.each($(tag)[0].files, function(i, file) {
            formData.append(tag.name, file);
        });
    });

    $.each(formParams, function(i, val) {
        formData.append(val.name, val.value);
    });

    return formData;
};

處理表單的symfony控制器方法如下所示:

/**
 * Matches /admin/upload/foto
 * @Route(
 *     "/upload/foto",
 *     options={"expose": true},
 *     name="admin_upload_foto")
 * @Method({"POST"})
 * @return JsonResponse
 */
public function upload_photo(
    Request $request
) {
    $response = new JsonResponse();

    $newPhoto = new Fotos();
    $photoForm = $this->createForm(PhotoType::class, $newPhoto);
    $photoForm->handleRequest($request);

    if ($photoForm->isSubmitted() && $photoForm->isValid()) {
        // This part is hit on the dev server
    } else if ($photoForm->isSubmitted()) {
        // This part is hit on the live server!
        // categorie contains no errors
        $photoForm['categorie']->getErrors()->__toString());
        // file contains an error: content-type is null;
        $photoForm['file']->getErrors()->__toString());
        $response->setStatusCode(400);
        $response->setData(array("result" => 0, "errors" => $errors));
    } else {
        $response->setStatusCode(400);
        $response->setData(array("result" => 0, "errors" => "Het foto-formulier is niet verzonden naar de server"));
    }
    return $response;
}

特定錯誤指出文件上傳文件的mime類型為null。 這是否意味着該文件永遠不會發送? 如果我這樣做: formData.getAll('file')我可以看到文件實際上在包里。

在Chrome的網絡分析器中,我可以看到有效載荷確實包含該文件,也許它在途中丟失了?

根據要求,這是實時服務器上$ _FILES的var_dump

array(1) {
  ["photo"]=>
  array(5) {
    ["name"]=>
    array(1) {
      ["file"]=>
      string(8) "test.png"
    }
    ["type"]=>
    array(1) {
      ["file"]=>
      string(9) "image/png"
    }
    ["tmp_name"]=>
    array(1) {
      ["file"]=>
      string(14) "/tmp/phpwy5m9y"
    }
    ["error"]=>
    array(1) {
      ["file"]=>
      int(0)
    }
    ["size"]=>
    array(1) {
      ["file"]=>
      int(25745)
    }
  }
}

任何幫助,將不勝感激

找到了解決方案,但我不得不放棄自動mime類型檢查。

根據symfony文檔,我不關心文件存儲為字符串。 symfony應該正確地“猜測”了啞劇類型檢查,但是針對啞劇類型的“斷言”規則不會選擇上載文件的啞劇類型。

我刪除了mimetype斷言規則,並使用手動擴展名檢查如下更改了我的處理方法:

    $newPhoto = new Fotos();
    $photoForm = $this->createForm(PhotoType::class, $newPhoto);
    $photoForm->handleRequest($request);
    //        var_dump($photoForm->getData());
    if ($photoForm->isSubmitted() && $photoForm->isValid()) {
        // $file stores the uploaded PDF file
        /* @var $file UploadedFile */
        $file = $newPhoto->getFile();
        $fileExtension = $file->guessClientExtension();
    //            var_dump($file->guessClientExtension());
        if (($fileExtension === 'png' || $fileExtension === 'jpeg' || $fileExtension === 'jpg')) {
           // Now I know the extension matches the type of files I want to handle
        } else {
           // Now I know the uploaded file is an invalid type
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM